SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
apple
Calcite | Level 5

 

 

Hi

 

Using SAS 7.1. A beginner in SAS. 

 

For the below solution, why do we need " %let month = %sysfunc(putn(&month,z2.));"? I understand that it is to convert the format to 2 length: 01, 02,...,12.

 

But why can't we do

"%do month = 01 %to 12;"

 

Thank you

 

%macro iterate;
%do year = 2010 %to 2015;
  %do month = 1 %to 12;
    /* assuming you want to zero fill the month */
    %let month = %sysfunc(putn(&month,z2.));
    %filecr(DE&YEAR.&MONTH.,&YEAR.,&MONTH.);
  %end;
%end;
%mend;

%iterate

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The iterated %do loop only understands numeric values. So even if you attempt %do i = 01 %to 10 the processor only uses the numeric value 1 not the character value 01. So using the %sysfunc creates a character 01 from the numeric 1 for use in your macro.

 

 

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20

I'm allways surprised when people ask "can/can't you do this", when they clearly have the possibility to try it on their own.

 

111  %macro iterate;
112  %do year = 2010 %to 2015;
113    %do month = 01 %to 12;
114      /* assuming you want to zero fill the month */
115      %let month = %sysfunc(putn(&month,z2.));
116      %put %nrstr(%filecr)(DE&YEAR.&MONTH.,&YEAR.,&MONTH.);
117    %end;
118  %end;
119  %mend;
120
121  %iterate
%filecr(DE201001,2010,01)
%filecr(DE201002,2010,02)
%filecr(DE201003,2010,03)
%filecr(DE201004,2010,04)
%filecr(DE201005,2010,05)
%filecr(DE201006,2010,06)
%filecr(DE201007,2010,07)
%filecr(DE201008,2010,08)
%filecr(DE201009,2010,09)
%filecr(DE201010,2010,10)
%filecr(DE201011,2010,11)
%filecr(DE201012,2010,12)
%filecr(DE201101,2011,01)
%filecr(DE201102,2011,02)
%filecr(DE201103,2011,03)
%filecr(DE201104,2011,04)
%filecr(DE201105,2011,05)
%filecr(DE201106,2011,06)
%filecr(DE201107,2011,07)
%filecr(DE201108,2011,08)
%filecr(DE201109,2011,09)
%filecr(DE201110,2011,10)
%filecr(DE201111,2011,11)
%filecr(DE201112,2011,12)
%filecr(DE201201,2012,01)
%filecr(DE201202,2012,02)
%filecr(DE201203,2012,03)
%filecr(DE201204,2012,04)
%filecr(DE201205,2012,05)
%filecr(DE201206,2012,06)
%filecr(DE201207,2012,07)
%filecr(DE201208,2012,08)
%filecr(DE201209,2012,09)
%filecr(DE201210,2012,10)
%filecr(DE201211,2012,11)
%filecr(DE201212,2012,12)
%filecr(DE201301,2013,01)
%filecr(DE201302,2013,02)
%filecr(DE201303,2013,03)
%filecr(DE201304,2013,04)
%filecr(DE201305,2013,05)
%filecr(DE201306,2013,06)
%filecr(DE201307,2013,07)
%filecr(DE201308,2013,08)
%filecr(DE201309,2013,09)
%filecr(DE201310,2013,10)
%filecr(DE201311,2013,11)
%filecr(DE201312,2013,12)
%filecr(DE201401,2014,01)
%filecr(DE201402,2014,02)
%filecr(DE201403,2014,03)
%filecr(DE201404,2014,04)
%filecr(DE201405,2014,05)
%filecr(DE201406,2014,06)
%filecr(DE201407,2014,07)
%filecr(DE201408,2014,08)
%filecr(DE201409,2014,09)
%filecr(DE201410,2014,10)
%filecr(DE201411,2014,11)
%filecr(DE201412,2014,12)
%filecr(DE201501,2015,01)
%filecr(DE201502,2015,02)
%filecr(DE201503,2015,03)
%filecr(DE201504,2015,04)
%filecr(DE201505,2015,05)
%filecr(DE201506,2015,06)
%filecr(DE201507,2015,07)
%filecr(DE201508,2015,08)
%filecr(DE201509,2015,09)
%filecr(DE201510,2015,10)
%filecr(DE201511,2015,11)
%filecr(DE201512,2015,12)



Data never sleeps
apple
Calcite | Level 5

Hi LinusH,

 

Please be polite.

 

I am trying to ask why I have to put month

 

%let month = %sysfunc(putn(&month,z2.));

 

 

If you look at the year variable, we don't have to do that.

 

Thank you

Ksharp
Super User

Answer is simple . You can not generate 01 02 ..... if you are using %do month = 01 %to 12;

 

%macro iterate;
%do year = 2010 %to 2015;
  %do month = 01 %to 12;
    %put &month ;
  %end;
%end;
%mend;

%iterate
1
2
3
4
5
6
7
8
9
10
11
12
 
 
 
ballardw
Super User

The iterated %do loop only understands numeric values. So even if you attempt %do i = 01 %to 10 the processor only uses the numeric value 1 not the character value 01. So using the %sysfunc creates a character 01 from the numeric 1 for use in your macro.

 

 

apple
Calcite | Level 5

Hi ,

 

Thank you for the reply. But I read https://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#p12zqzvwx4...

that

PUTN Function

Enables you to specify a numeric format at run time.

 

Hence does it change a numeric variable to a character variable?

 

Thank you

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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