Created
May 2, 2014 04:08
-
-
Save plpxsk/29d362be4b2a519de861 to your computer and use it in GitHub Desktop.
SAS: quickly generate indicator variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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