- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You cannot apply a numeric format to the letter I.
You forgot the &.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%let month_input=%sysfunc(putn(&i.,z2.));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
this is the error message got using %let month_input=%sysfunc(putn(&i.,z2.));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;