How to transform (transpose) a dataset in SAS, so that variables names become class identifiers? -
this question has answer here:
how transform, of sas code, following table:
id t u 1 1253 1349 2 1139 1382 3 1633 1663 4 1372 1541 5 1502 1335 into table?
classid t 1253 t 1139 t 1633 t 1372 t 1502 u 1349 u 1382 u 1663 u 1541 u 1335 ps: did try using sas sql (join all), not convert t , u variables class identifiers)
to avoid hard coding values, create array loops through each record , uses vname function read current variable name. variable name assigned classid , variable value assigned all. code works if variables numeric, you'll need tweak if have mixture of numeric , character variables (just create array character variables if that's case).
/* create initial dataset */ data have; input id t u; datalines; 1 1253 1349 2 1139 1382 3 1633 1663 4 1372 1541 5 1502 1335 ; run; /* transform data required dataset */ data want (keep=classid all); /* keep wanted variables */ set have (drop=id); /* don't read in id it's not needed */ array vars{*} _numeric_; /* set array of remaining variables (assume numeric) */ = 1 dim(vars); /* loop through each value */ classid=vname(vars{i}); /* set classid current variable name */ = vars{i}; /* set current value */ output; end; run; /* sort data classid */ proc sort data=want; classid; run;
Comments
Post a Comment