Skip to content

Instantly share code, notes, and snippets.

@statgeek
statgeek / SAS_ods_graphics_enabled.sas
Created September 10, 2018 19:27
SAS - check if ODS GRAPHICS is enabled
/*This program checks if the ODS GRAPHICS option is enabled
Originally from:
https://communities.sas.com/t5/ODS-and-Base-Reporting/Can-you-query-if-ODS-GRAPHICS-is-on-or-off/m-p/494271
*/
*check current status;
%put NOTE:- Current Status = &sysodsgraphics;
@statgeek
statgeek / data2datastep.sas
Last active September 12, 2018 02:59
SAS - create sample data
%macro data2datastep(dsn=,lib=,outlib=,file=,obs=,fmt=,lbl=);
%local varlist fmtlist inputlist msgtype ;
%if %superq(obs)= %then %let obs=MAX;
/*added by FKhurshed to set linesize to max.
This allows the macro to output the datalines correctly to the
log and not run over lines. Current value is stored here to reset
at the end*/
%let LS= %sysfunc(getoption(LS));
@statgeek
statgeek / summary_table.sas
Last active October 2, 2018 14:59
SAS Summary table chi square
/*--------------------------------------------------------------------------------*/
/* Author: F. Khurshed */
/* Date: June 17, 2011 */
/* Purpose: This macro creates a set of chisquare tables appended together. */
/* It includes the chisquare value. */
/* Modified by: F. Khurshed */
/* Date: January 17, 2012 */
/* Purpose: Adding in the ability for the macro to add the total column. */
@statgeek
statgeek / sas_stack_data_macro_SAS_91.sas
Last active July 27, 2018 17:07
SAS - stack data with dates using a macro
*sample data sets for demonstration;
data price_20080131;
set sashelp.class;
test=1;
run;
data price_20080229;
set sashelp.class;
test=2;
run;
@statgeek
statgeek / sas_variable_list_compare.sas
Created July 26, 2018 18:12
SAS - which variables are missing from the data set in comparison to another data set
*create example data;
data class;
set sashelp.class;
run;
data class2;
set sashelp.class;
drop age sex;
run;
@statgeek
statgeek / sas_proportion_confidence_intervals.sas
Created July 19, 2018 20:34
Find the confidence intervals for proportions
/*This program calculates binomial percentages and confidence limits.
Howver, this has no correction methodology so the lower bound can go below 0.
In this case a different method is needed*/
*set table name to summarize;
%let dsin=sashelp.class;
*set variable name to get percentages;
%let var = sex;
*set name of output data set;
@statgeek
statgeek / sas_unique_combinations.sas
Created June 15, 2018 02:56
SAS - Combinations - Using LEXCOMBI to generate unique combination lists
/*this creates a list of combinations, designed for numeric variables*/
%let n=5; /* total number of items */
%let k=2; /* number of items per row */
/* generate combinations of n items taken k at a time */
data Comb(keep=c1-c&k);
array c[&k] (&k.*0); /* initialize array to 0 */
array list[&n] _temporary_ (1, 2, 3, 4, 5);
ncomb = comb(&n, &k); /* number of combinations */
do j = 1 to ncomb;
@statgeek
statgeek / sas_var_exist.sas
Created June 15, 2018 02:38
SAS - check for the existence of specific variables in a data step
/*This program checks if two variables exist in a data step and then does conditional logic
if the variables are present
This is also an example of CALL VNEXT()*/
/*Generate sample data*/
*both variables exist;
data class1;
@statgeek
statgeek / sas_mlf_format
Created June 11, 2018 23:59
SAS - formats - multilabel formats
/*This program shows how an multilabel format works and that the summary statistics can be done at a unique level but also aggregated at a higher level included in the data.*/
/*sample data*/
data have;
input Year Area $ Profit;
cards;
2001 A 1
2002 A 2
2001 B 1
2001 C 3
@statgeek
statgeek / sas_create_macro_variables_from_dataset.sas
Last active August 19, 2021 18:45
SAS - Create Multiple Macro Variables from a data set
/*This is an example of how to create multiple macro variables in
one SQL step.*/
*note that you do not need to specify the number of items;
*SAS will do that portion for you;
proc sql noprint;
select name into :name1- from sashelp.class;
quit;
%put &name1;