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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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