DATA Step, Macro, Functions and more

MAcro VAriable strips leading zero

Reply
Contributor
Posts: 27

MAcro VAriable strips leading zero

Wondering if someone could help me with the following:

rsubmit;
%let date1=0905;
%let date2=0906;
endrsubmit;

rsubmit;
%macro Asset_history( Start= , Stop= );

%do j = &Start %to &Stop;

proc sql;
create table HH_Assets_&j as
select nnn, cddd, sum(aaaa) as value_&j,sum(bbb) as qty_&j,
from XYZ.part&j inner join Elig_secs on
part&j..id=Elig_secs.id
and aaa > 0 and cdd='A'
group by nnn,cdd,
order by nnn,cdd;
quit;
proc sort data = HH_Assets_&j; by nnn cdd; quit;

%end;
%mend;
endrsubmit;


rsubmit;
%Asset_history( Start=&date1 , Stop=&date2);
endrsubmit;


I keep getting an error message - it strips the leading zero. Could someone please help!
SAS Super FREQ
Posts: 8,743

Re: MAcro VAriable strips leading zero

Hi:
As it says in the doc:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a000208971.htm
"All parts of the macro language that evaluate expressions (for example, %IF and %DO statements) call %EVAL to evaluate the condition. "

And, if you look at the %DO documentation,
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a000543755.htm

you will see that the start and stop values for %DO must resolve to be:
"...specify integers or macro expressions that generate integers to control the number of times the portion of the macro between the iterative %DO and %END statements is processed."

Therefore, the string 0905 as an INTEGER number is 905.

You can prove this to yourself by turning on the macro debugging options:
options symbolgen mprint mlogic;

I'm betting you will see that &START and &STOP still have the leading 0, but that &J (since it must resolve to an integer number) will be either 905 or 906.

That means you will have to control how &J gets used in building your filenames and variable names. Probably you will need to create another macro variable one that you would use just in the names.

cynthia
[pre]
options mprint symbolgen mlogic;
%macro Asset_history( Start= , Stop= );

%do j = &Start %to &Stop;

** put 0 in MYJ for use in names;
%let myj = %sysfunc(putn(&j,z4.));
%put ***** myj= &myj;

proc sql;
create table HH_Assets_&myj as
.... use &myj instead of &j in the names ...
quit;

... rest of code ...
%end;

%mend;

** test with more values;
%let date1=0905;
%let date2=0908;
%Asset_history( Start=&date1 , Stop=&date2);
[/pre]
Contributor
Posts: 27

Re: MAcro VAriable strips leading zero

Thank you Cynthia!!
Frequent Contributor
Posts: 91

Re: MAcro VAriable strips leading zero

Another simple solution is this case is to use 200905 and 200906.
Super Contributor
Posts: 359

Re: MAcro VAriable strips leading zero

For what it is worth:
This brings up a second reason this code bugs me. Is "0905" May 2009 or September 5? If the latter what happens to the code on Sept 30? Stepping through a string as if it is an integer just seems full of potential problems to me.
Contributor
Posts: 27

Re: MAcro VAriable strips leading zero

Thank you all for your feedback. The sas input datasets have the suffix yymm and therefore I am forced to adopt the formats listed above. Cynthia's suggestion worked like a charm!
Super Contributor
Super Contributor
Posts: 3,174

Re: MAcro VAriable strips leading zero

Just a tad curious what you will do when crossing a year boundary, where one string is not in the same year as the other?

Suggest you still take in the two macro variables but drive the %DO / %END process using SAS DATE values represented as macro variables, and then truncate the output format of your low "yymm" and high "yymm" data strings as needed.

This would mean using %SYSFUNC and PUTN / INPUTN to convert the yymm to yymm01, using INPUTN and then use PUTN, if needed, to generate a truncated DATE variable string. You will need to consider using the INTNX function for your processing, possibly.

Some examples:

%LET YYMM = 0905;
%LET MYDATE = %SYSFUNC(INPUTN(&YYMM.01,YYMMDD.));
%LET MYDDMMYYYY = %SYSFUNC(PUTN(&MYDATE,DATE7.));
%LET MYYYMM = %SYSFUNC(PUTN(&MYDATE,YYMMN4.));
%PUT _USER_;

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 6 replies
  • 1020 views
  • 0 likes
  • 5 in conversation