SAS dynamic variable names from other variables -
i trying create sas variable names based on data contained within other variables. example, start
obs var1 var2 1 abc x 2 def x 3 ghi y 4 jkl x and end with
obs var1 var2 x y 1 abc x abc 2 def x def 3 ghi y ghi 4 jkl x jkl i have 1 way of doing requires ugly macros first create variables needed (using length statement) , creating whole series of numbered macro variables (1 per observation) later called inside data step loop. works complicated , don't think scale real data, contain multiple variables creation per row, , few thousand rows.
i've tried arrays - saving variables names in macro var, using generate array statement, , trying keep track of array index needed each new variable, complicated.
what analogous
vvaluex(var2)=var1 except vvaluex can't on left-hand side of equals. thoughts or ideas?
proc transpose handy way example in question.
data have; input obs var1 $ var2 $; datalines; 1 abc x 2 def x 3 ghi y 4 jkl x ;;;; run; proc transpose data=have out=want; obs; id var2; var var1; copy var1 var2; run; another option similar you've tried before, using arrays , vname:
proc sql; select var2 :var2list separated ' ' have; quit; data want; set have; array newvars $ &var2list; _t = 1 dim(newvars); if vname(newvars[_t]) = var2 do; newvars[_t] = var1; leave; end; end; run; proc transpose should faster , more flexible, might work better purposes.
Comments
Post a Comment