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

Hi, I'm trying this code:

 

%macro month(L1);
%let L1=&L1;
%let L2=L1+7;
%let L3=L2+7;
%let L4=L3+7;

 

data L1;
set interval.d_&L2;
keep _LABEL_ d_&L2;
run;

%mend;

 

%month(20499);

 

The proble is that L2=20499+7; and its a error because the dataset interval.d_20499+7 does not exist. I need that L2=20506

 

Any ideas??

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Since the Macro language is primarily a text generator you need to tell when you are doing calculations. You also need to use &L1 to refer to the assigned value of the macro variable:

 

Try

%let L2= %eval ( &L1+7);

and similar.

 

Note that %eval only does integer arithmetic. You will need %sysevalf to do decimals.

 

Excessive calculations in the macro language is not a good idea...

View solution in original post

2 REPLIES 2
ballardw
Super User

Since the Macro language is primarily a text generator you need to tell when you are doing calculations. You also need to use &L1 to refer to the assigned value of the macro variable:

 

Try

%let L2= %eval ( &L1+7);

and similar.

 

Note that %eval only does integer arithmetic. You will need %sysevalf to do decimals.

 

Excessive calculations in the macro language is not a good idea...

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The question is, do you understand the difference between macro and datastep?  Datastep is the SAS language, it is designed to have fiull functionality, all data structures, operations and such like need to manipulate data.  Macro on the other hand has none of this.  It is purely there to generate some text which would resolve to be datastep.  It has no data structures other than text, and no functionality within itself to process data.  

The next question would be, why is the data all split up, with data actually appearing as dataset name.  Thats an innefficient methodology from a programming perspective.  Far simpler to do:

data all_data;
  format dte date9.;
  set interval.d: indsname=nme;
  dte=input(nme,best.);   /* set whatever the format of 20499 is? */
run;

Not sure what the 20499 stands for in your example, maybe its a numeric date, then best should do it. You then have one dataset with a variable to indicate date timestamp.  Then you can use datasteps, interval functions etc.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 3226 views
  • 0 likes
  • 3 in conversation