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

though the following command works well

%let month_input=%sysfunc(putn(1,z2.));
%put &month_input;

the result is 02 as expected,

 

however when I put it into loop, the putn is not accepted.

 

data combine;
do i=1 to 12;
%let month_input=%sysfunc(putn(i,z2.));
%put &month_input;
month=&month_input;
output;
end;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Do not use macro functions in general to modify or attempt to use dataset variables. The timing is wrong as to when/what executes and the macro processor does not see the data vector.

 

Are you trying to create a character variable with leading 0 or a numeric variable?

 

data combine;
do i=1 to 12;
   monthchar= put(i,z2.);
   output;
end;
run;

If you create variable in a dataset there are not leading zeroes unless 1) the values are between (0,1) such as 0.25 :example:

data example;
   x=0.25;
   y=0004;
run;

OR you apply the Z format to a variable:

data example;
   x=0.25;
   y=0004;
   format y z4.;
run;

View solution in original post

8 REPLIES 8
Tom
Super User Tom
Super User

You cannot apply a numeric format to the letter I.

You forgot the &.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

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

fsuzhang
Fluorite | Level 6
Argument 1 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
this is the error message got using %let month_input=%sysfunc(putn(&i.,z2.));
novinosrin
Tourmaline | Level 20

Hi @fsuzhang  Are you trying to do this?

 

%let month_input=%sysfunc(putn(1,z2.));
%put &month_input;

data combine;
do i=1 to 12;
month_input=putn(i, "z2.");
put month_input= ;
month=month_input;
output;
end;
run;

 OR

/*macro loop*/

%macro putn;
%do i=1 %to 12;
%let month_input=%sysfunc(putn(&i, z2));
%put month=&month_input;
%end;
%mend putn;

%putn
fsuzhang
Fluorite | Level 6

Thanks for the solution, this is not what I want, I need it to be done in data step

 

data combine;
%let name1=diabetes;
do i=1 to 12;
i=put(i,z2.);%let month_input=%sysfunc(putn(i, z2.));
%put &month_input;

%let name=%sysfunc(catx(&name1., &month_input));

%put &name.;

 

set &name;
end;
run;

 

In other word I need to combine 12 datasets which name ended with 2-digit month, however the  putn is not functional in data step.

fsuzhang
Fluorite | Level 6

Thanks all guys, I solve it finally using macro outside data step

 

%let name1=diabetes_;
%let year=2018;
%macro putn;
data combine;
%do i=1 %to 12;
%let month_input=%sysfunc(putn(&i, z2));
%put month=&month_input;
%let name= &name1&month_input.&year;
%put &name;

date=&month_input.&year.;
set &name.;
output;

%end;
run;
%mend putn;

%putn

 

but I am still wondering with why the following code not working

 

data null;
do i=1 to 12;
%let month_input=%sysfunc(putn(i, z2.));
%put &month_input;

month=&month_input;

run;

Tom
Super User Tom
Super User

@fsuzhang wrote:

but I am still wondering with why the following code not working

 

data null;
do i=1 to 12;
%let month_input=%sysfunc(putn(i, z2.));
%put &month_input;

month=&month_input;

run;


Because the letter I is NOT a number.

It is also NOT the name of variable when the macro processor is running.

The macro processor is used to process text.  The text that it generates is THEN passed to the SAS compiler to interpret as code.

 

Even if you could have could have gotten the macro processor to generate the macro variable MONTH_INPUT its value would have been resolved by the macro processor BEFORE the code is passed to SAS to run.

So you are trying to generate a data step like this:

data out;
do i=1 to 12;
  month=12;
end;
run;
ballardw
Super User

Do not use macro functions in general to modify or attempt to use dataset variables. The timing is wrong as to when/what executes and the macro processor does not see the data vector.

 

Are you trying to create a character variable with leading 0 or a numeric variable?

 

data combine;
do i=1 to 12;
   monthchar= put(i,z2.);
   output;
end;
run;

If you create variable in a dataset there are not leading zeroes unless 1) the values are between (0,1) such as 0.25 :example:

data example;
   x=0.25;
   y=0004;
run;

OR you apply the Z format to a variable:

data example;
   x=0.25;
   y=0004;
   format y z4.;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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