* Reshaping long-to-wide and the data step; * Suppose I'd like to have bar charts of marital status at each wave, and all with common axes for easy comparison. An easy was to do this would be with proc sgpanel ... but I need my data in long (groups of observations) form.; libname y "y:\sas\arrays"; libname library (y); data nlslong; set y.nlswomen; id= _n_; * No ID var, so create one - this will be useful later; array mstatus{*} R0002400 R0133700 R0205100 R0288200 R0308300 R0367600 R0455600 R0491300 R0661400 R0666600 R0721700 R0869900 R0997700 R1290700 R1664710 R3507200 R4278200 R5447500 R6516200; *You don't need to count the variables; do wave=1 to dim(mstatus); marital = mstatus{wave}; * move everything into one var; output; *create multiple output obs per 1 input obs; end; drop R0002400 -- R6516200; * You don't need to keep i, do you?; run; proc freq; tables wave*marital / nopercent nocol; run; * Having reshaped the data, the plot is extremely simple; proc sgpanel; panelby wave; vbar marital; run; /* Long to wide */ * The data need to be sorted.; /*proc sort data=nlslong; by id wave; run;*/ * I'll illustrate this in three stages, but in practice you just use the last stage.; /* Stage 1: moving data values to separate columns */ * This is where the array is used; data nlswide; set nlslong; array mstatus{19}; mstatus{wave}=marital; run; /* Stage 2: Retaining data values across observations */ data nlswide; set nlslong; array mstatus{19}; retain mstatus1 - mstatus19; * this retains values across observations; mstatus{wave}=marital; run; /* Stage 3: Keeping the final observation in each by-group */ data nlswide(drop=wave marital); set nlslong; by id; * set by group; array mstatus{19}; retain mstatus1 - mstatus19; mstatus{wave}=marital; if last.id; * just the last observation in each group; run; proc freq; tables mstatus1-mstatus19 / nocum; run; * Further: -If data are not balanced, need to reset first.id to missing -Use Proc Transpose ;