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.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: 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.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
einstein
Quartz | Level 8
Thank you! This worked perfectly!

sas-innovate-2024.png

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!

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.

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
  • 4 replies
  • 803 views
  • 0 likes
  • 3 in conversation