Skip to content

Instantly share code, notes, and snippets.

@plpxsk
Created May 2, 2014 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plpxsk/29d362be4b2a519de861 to your computer and use it in GitHub Desktop.
Save plpxsk/29d362be4b2a519de861 to your computer and use it in GitHub Desktop.
SAS: quickly generate indicator variables
/* macro to convert variables to indicators*/
/* outputs dataset "df" with patid and the indicator only for easy later merge*/
/* v = var for indicator // n = name of new var(use same prefix with consecutive #s!!) */
%MACRO INDX(v=, n=);
data df;
set bmd;
if &V > . then tt = 1; else tt =0;
rename tt=&N;
label
&N = "&V 0/1"
;
run;
%MEND INDX;
/* put all dataset variable names into macro list */
/* source: "Putting SAS Dataset Variable Names into a Macro Variable cc01" */
/* search online */
proc contents data = my_data
out = vars(keep = varnum name)
noprint;
proc sql noprint;
select distinct name
into :orderedvars separated by ‘ ’
from vars
order by varnum;
quit;
/* run macro for each variable in above dataset*/
%macro RUNIT;
%local i cur;
%do i=1 %to %nitem(&maclist);
%let cur=%scan(&maclist,&i);
%INDX(v=&CUR, n=catx(v,&i));
%end;
%mend RUNIT;
%RUNIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment