BookmarkSubscribeRSS Feed
SAS09
Calcite | Level 5
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!
6 REPLIES 6
Cynthia_sas
SAS Super FREQ
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]
SAS09
Calcite | Level 5
Thank you Cynthia!!
advoss
Quartz | Level 8
Another simple solution is this case is to use 200905 and 200906.
Flip
Fluorite | Level 6
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.
SAS09
Calcite | Level 5
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!
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.

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!

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