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

Hello,

I have the following dataset

 

var 1 var2 var3 var4

region 1 2000 num1 num2

region1 2001 num3 num4

region1 32002 num5 num6

...

region1 2018 num19 num20

region2 2000 num21 num22

region2 ...

 

regionn...

I need to create a new variable for each region for each year which computes the sum of var3 for year i + var4 for year (i+1)

 

Since I have 100 regions and periods, I want a macro...

 

I tried to create dummy variables for each year but I don't know how to invoke the dummy for year(i+1)

and I don't know what to do with my dummies...

 

%macro EWD;
%DO i=1950 %TO 2016;
%put bin%eval(&i+1);
sum=var3*bin&i+var4*bin%eval(&i+1));
%end;
%mend EWD_2;

data new;
set essi;
%EWD_2;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You don't need a macro at all. Macros create dynamic code and are not for handling data.

 

Either sort by region and descending year, and use the lag() function, or merge the dataset to itself with firstobs=2, thereby creating a lookahead.

For code, please post data in a datastep; see my footnotes for help.

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry your post is very hard to follow.  Post test data in the form of a datastep:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

We only need a few rows that we can run and then have something to code against.  Then show what the output should look like.

 

What I will say off the bat is this, Macro is never needed.  Almost all the macro coding I see can be replaced by simply understanding the data and how to model it correctly.  It sounds in this instance, you will be able to do what you want by either using retained variables, or by using proc summary or one of the procedures designed for the purpose, but without some clear example its hard to say.

Kurt_Bremser
Super User

You don't need a macro at all. Macros create dynamic code and are not for handling data.

 

Either sort by region and descending year, and use the lag() function, or merge the dataset to itself with firstobs=2, thereby creating a lookahead.

For code, please post data in a datastep; see my footnotes for help.

CarolineDP
Calcite | Level 5

Using the lag function was enough! Thank you!

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