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

I'm trying to create a list of statistics for all months within my data set. I have everything by month so just need to assign the values to be manipulated further on. The code I'm trying to use is below, but it's not working as it says there is no corresponding %to statement for the initial %do.

 

%do stat='sum_of_x','sum_of_y','count_of_x','count_of_y','total_z';
   %do y=13 %to 18;
      %do m=1 %to 12;
         %let mym = %sysfunc(putn(&m,z2.));
         %if &y&mym<=1802 %then %do;
            ,sum(&stat) as &stat_&y&mym
         %end;
      %end;
   %end;
%end;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If you want to perform a macro language %DO loop over a list of values, you have to program it yourself.  Here's some help:

 

https://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-p...

 

 

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The %do construct does not have the functionality to do lists of values, that is a datastep do loop only.  

Why are you doing it this way, Base SAS is the programming language and has the full functionality, macro is not a replacement for this.

If you want sums by month, then you would use proc means/summary, by month, then if you need a transposed output for a report (as working with transposed data is more complicated than with normalised data) you can use proc transpose to transpose the data at that point.  Zero need for macro, smaller easier to maintain code, and faster as it is using the actual programming language.

if you provide some test data I can show you the code. 

Astounding
PROC Star

If you want to perform a macro language %DO loop over a list of values, you have to program it yourself.  Here's some help:

 

https://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-p...

 

 

Reeza
Super User

Consider using PROC MEANS or SUMMARY instead, they're typically a lot more flexible in how you can calculate summary statistics. 

 


@bethsmith wrote:

I'm trying to create a list of statistics for all months within my data set. I have everything by month so just need to assign the values to be manipulated further on. The code I'm trying to use is below, but it's not working as it says there is no corresponding %to statement for the initial %do.

 

%do stat='sum_of_x','sum_of_y','count_of_x','count_of_y','total_z';
   %do y=13 %to 18;
      %do m=1 %to 12;
         %let mym = %sysfunc(putn(&m,z2.));
         %if &y&mym<=1802 %then %do;
            ,sum(&stat) as &stat_&y&mym
         %end;
      %end;
   %end;
%end;


 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 669 views
  • 0 likes
  • 4 in conversation