BookmarkSubscribeRSS Feed
Calcite | Level 5



I currently have a program that outputs a new data set that contains results of the proc surveyfreq procedure below: 

proc surveyfreq data=2016;
weight finalwt;
table (gender grade age race hhincome)*(eversmk cursmk)/ cl row nocellpct nostd nofreq nowt nototal; ods output CrossTabs=table1;

data table1;
retain gender grade age race hhincome eversmk cursmk RowPercent RowLowerCL RowUpperCL; set table1(drop=F_cursmk LowerCL UpperCL Table _SkipLine); run;

I would like to develop a macro that calls in different years of data instead of repeating the same surveyfreq procedure on each year of data I have. The above program is for 2016 data, but I would like a macro that calls in 2015, 2014, etc and outputs the same resulting statistics. I have little experience with developing macro code, so any advice would be appreciated. Thank you!

Pyrite | Level 9

You can create a macro variable like this:

%let year=2016;

If you have all the years in one data set, you can combine this with a where statement like so:

proc surveyfreq data-yourdata;
    where year=&year;
    other code;

Unless there's a reason you need it to be separate you may be better off by using a BY statement instead though. Then you would just run the proc once, and get all of your output in one place.

proc surveyfreq data=yourdata;
    BY year;
    other code;
Super User

1) combine the data sets

2) make sure you have two variables with the year value for each of the data sets

3) One of those variables goes on a STRATA statement

4) the Other Year variable goes in your tables statement to get a summary by year. (can't have a variable on the strata statement as a tables variables)


No macro needed




Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.


Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 4 in conversation