Folks,
Can anyone point out why my code is not working. I've created a macro which loops through 10 iterations to calculate statistics. I want to use proc means to calculate various descriptive stats for each group but for some reason only the last observation (10) gets outputted.
%macro State (year, DName,combined_sample);
%do number = 1 %to 10;
data i&year ;
set &combined_sample.;
where interview='1';
where decile_equiv_inc=&number;run;
*title &Year &number;
*proc means data=i&year median mean ;
*class decile_equiv_inc;
*var measure_error_tot measure_error_disp;
*by decile_equiv_inc;
*output out=analysis_&year
median= measure_error_tot_1 measure_error_disp_5
;
run;
/*data h;
set analysis_2013
analysis_2014
analysis_2015
;run;*/
*proc sort data=i&year ; *by decile1;
proc means data=i&year median mean sum ;
where interview='1';
weight euroweight;
var eq_inc1nat eq_inc1nat_admin eq_totinc_survey eq_totinc_admin;
by decile_equiv_inc var ;
output out=med_inc_state_&year. ;
median= eq_totinc_survey_median eq_inc1nat_median eq_totinc_admin_median eq_inc1nat_admin_median
mean= eq_totinc_survey_mean eq_inc1nat_mean eq_totinc_admin_mean eq_inc1nat_admin_mean
sum= eq_totinc_survey_sum eq_inc1nat_sum eq_totinc_admin_sum eq_inc1nat_admin_sum;
run;
data med_inc_state_&year.;
set med_inc_state_&year.;
year=&year.;
data analysis;
set med_inc_state_2013
med_inc_state_2014
med_inc_state_2015;run;
%end;
%mend State;
%State (2013,i2013,combined_sample_2013_7);
%State (2014,i2014,combined_sample_2014_7);
%State (2015,i2015,combined_sample_2015_7);
replace the below code:
data analysis;
set med_inc_state_2013
med_inc_state_2014
med_inc_state_2015;run;
with this code:
proc append base=analysis data= med_inc_state_&year. force;
run;
Thanks,
Vish
You have a do loop which iterates the code 10 times, however, nothing within that code actually change each iteration, i.e. exactly the same code is run 10 times, meaning that each dataset is overwritten 10 times, the only change being that a different set of data is included in the where clause, thus, only the last iteration data is saved. A lot of what you have written doesn't make sense, you pass in year, but don't use it on any criteria, so it never effects - just an example.
Seems like a big faff just to get some output though. Explore by group processing and how it can save you countless hours programming and writing macro code. For instance in this dataset:
set &combined_sample.;
Which I cant see, you can apply certain groups to the data, perhaps year as that seems to be the driving factor in the loop. Then its simply a matter of proc means by year. This will give you one output for each year - no looping needed. Short simple code. If you provide test data in the form of a datastep and required output I can show you.
Try this:
%macro State (year, DName,combined_sample);
%do number = 1 %to 10;
data i&year ;
set &combined_sample.;
where interview='1';
where decile_equiv_inc=&number;
run;
*title &Year &number;
*proc means data=i&year median mean ;
*class decile_equiv_inc;
*var measure_error_tot measure_error_disp;
*by decile_equiv_inc;
*output out=analysis_&year
median= measure_error_tot_1 measure_error_disp_5
;
/*run;*/
%end;
/*data h;
set analysis_2013
analysis_2014
analysis_2015
;run;*/
*proc sort data=i&year ; *by decile1;
proc means data=i&year median mean sum ;
where interview='1';
weight euroweight;
var eq_inc1nat eq_inc1nat_admin eq_totinc_survey eq_totinc_admin;
by decile_equiv_inc var ;
output out=med_inc_state_&year. ;
median= eq_totinc_survey_median eq_inc1nat_median eq_totinc_admin_median eq_inc1nat_admin_median
mean= eq_totinc_survey_mean eq_inc1nat_mean eq_totinc_admin_mean eq_inc1nat_admin_mean
sum= eq_totinc_survey_sum eq_inc1nat_sum eq_totinc_admin_sum eq_inc1nat_admin_sum;
run;
data med_inc_state_&year.;
set med_inc_state_&year.;
year=&year.;
proc append base=analysis data= med_inc_state_&year. force;
run;
%mend State;
%State (2013,i2013,combined_sample_2013_7);
%State (2014,i2014,combined_sample_2014_7);
%State (2015,i2015,combined_sample_2015_7);
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.