First, your example has 2 seemingly small but significant errors: proc sort data=short_state_soc22 ; by state; run; %macro srt(); data wide_state; set short_state_soc22; by state; array soc22{22} occ_11 occ_13 occ_15 occ_17 occ_19 occ_21 occ_23 occ_25 occ_27 occ_29 occ_31 occ_33 occ_35 occ_37 occ_39 occ_41 occ_43 occ_45 occ_47 occ_49 occ_51 occ_53 ; %do i=11 %to 53 %by 2; retain soc22; if soc_22 ="&i" %then soc22[occ_&i]=count; *"%then" is wrong, should be "then"; %end; if last.state=1 %then output; *"%then" is wrong, should be "then"; run; %mend srt; %srt; Remember that the macro facility is just a code generator. What your macro does looks like this: data wide_state; set short_state_soc22; by state; array soc22{22} occ_11 occ_13 occ_15 occ_17 occ_19 occ_21 occ_23 occ_25 occ_27 occ_29 occ_31 occ_33 occ_35 occ_37 occ_39 occ_41 occ_43 occ_45 occ_47 occ_49 occ_51 occ_53 ; retain soc22; if soc_22 ="11" then soc22[occ_11]=count; retain soc22; if soc_22 ="13" then soc22[occ_13]=count; retain soc22; if soc_22 ="15" then soc22[occ_15]=count; retain soc22; if soc_22 ="17" then soc22[occ_17]=count; retain soc22; if soc_22 ="19" then soc22[occ_19]=count; retain soc22; if soc_22 ="21" then soc22[occ_21]=count; retain soc22; if soc_22 ="23" then soc22[occ_23]=count; retain soc22; if soc_22 ="25" then soc22[occ_25]=count; retain soc22; if soc_22 ="27" then soc22[occ_27]=count; retain soc22; if soc_22 ="29" then soc22[occ_29]=count; retain soc22; if soc_22 ="31" then soc22[occ_31]=count; retain soc22; if soc_22 ="33" then soc22[occ_33]=count; retain soc22; if soc_22 ="35" then soc22[occ_35]=count; retain soc22; if soc_22 ="37" then soc22[occ_37]=count; retain soc22; if soc_22 ="39" then soc22[occ_39]=count; retain soc22; if soc_22 ="41" then soc22[occ_41]=count; retain soc22; if soc_22 ="43" then soc22[occ_43]=count; retain soc22; if soc_22 ="45" then soc22[occ_45]=count; retain soc22; if soc_22 ="47" then soc22[occ_47]=count; retain soc22; if soc_22 ="49" then soc22[occ_49]=count; retain soc22; if soc_22 ="51" then soc22[occ_51]=count; retain soc22; if soc_22 ="53" then soc22[occ_53]=count; if last.state=1 then output; run; Decide for yourself which code is more efficient and readable! All those ifs eat a lot of CPU power, BTW. The macro facility lets you create dynamic code. If the code remains static, do it in the data step itself. Edit: replaced %then with then
... View more