DATA Step, Macro, Functions and more

Count Variable inside Macro

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Count Variable inside Macro

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??


Accepted Solutions
Solution
‎11-02-2016 06:38 PM
Super User
Posts: 11,343

Re: Count Variable inside Macro

Posted in reply to mariange8282

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


All Replies
Solution
‎11-02-2016 06:38 PM
Super User
Posts: 11,343

Re: Count Variable inside Macro

Posted in reply to mariange8282

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...

Super User
Super User
Posts: 7,988

Re: Count Variable inside Macro

Posted in reply to mariange8282

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 231 views
  • 0 likes
  • 3 in conversation