BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
einstein
Quartz | Level 8

I have a macro that reads through a list of states and years, however, I'm wondering if there's a more efficient way to read through the states instead of only using a macro.

 

Here is my code:

 

%macro test (st=,year=);

proc freq data=data.out_&st._&year.;
tables place;
run;
%mend test;
%test (st=NY, year=2011);
%test (st=NY, year=2012);
%test (st=NY, year=2013);
%test (st=VA, year=2011);

etc.

Is there an easier, more consise way to run through the years without having to have three lines for NY for each year?  Should I use an array?

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Is the list of years the same for every state?  If so, then it should be straight forward to add some %DO loops in your macro.

 

So instead of calling it once for each freq, you could call it like:

%test (st=NY VA RI, startyear=2011, stopyear=2013)

 

That doesn't change the execution efficiency, but it does make it easier to generate the set of PROC FREQ steps without writing each macro call yourself.

 

Another option would be to have a dataset that has one record per freq table you want, with variables State and Year.  Then you can use that dataset to drive your macro.  For example, using CALL EXECUTE to read a record from the dataset and generate a macro call.

 

Even if there are millions of records, I'd still consider putting them all together and indexing the dataset.

The Boston Area SAS Users Group (BASUG) is hosting an in person Meeting & Training on June 27!
Full details and registration info at https://www.basug.org/events.

View solution in original post

4 REPLIES 4
Reeza
Super User

Why is your data split like that in the first place?

Perhaps combining them from the start would help.

 

Unless your datasets are in the millions of rows each or possibly tens, there isn't a need to separate the data like that.

einstein
Quartz | Level 8

Yep, these datasets have millions of observations, which is why they're split by state and year.

Quentin
Super User

Is the list of years the same for every state?  If so, then it should be straight forward to add some %DO loops in your macro.

 

So instead of calling it once for each freq, you could call it like:

%test (st=NY VA RI, startyear=2011, stopyear=2013)

 

That doesn't change the execution efficiency, but it does make it easier to generate the set of PROC FREQ steps without writing each macro call yourself.

 

Another option would be to have a dataset that has one record per freq table you want, with variables State and Year.  Then you can use that dataset to drive your macro.  For example, using CALL EXECUTE to read a record from the dataset and generate a macro call.

 

Even if there are millions of records, I'd still consider putting them all together and indexing the dataset.

The Boston Area SAS Users Group (BASUG) is hosting an in person Meeting & Training on June 27!
Full details and registration info at https://www.basug.org/events.
einstein
Quartz | Level 8
Thank you! This worked perfectly!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1602 views
  • 0 likes
  • 3 in conversation