Solved
Contributor
Posts: 23

# 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: 13,941

## Re: Count Variable inside Macro

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

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

## Re: Count Variable inside Macro

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
Posts: 9,842

## Re: Count Variable inside Macro

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.