BookmarkSubscribeRSS Feed
Toni2
Lapis Lazuli | Level 10

hi i am trying using a macro do loop to populate some variables.

 

More specifically, when i run the code (see below) for 10 loops the observations are moving down in data (see below this is after 10 times loop) which is not what i need.

 

I need in every loop to add at the end of each column the additional observations from the calculations inside the macro. 

 

For example, at the beginning of the 2nd loop var17 must have all its observations plus the additional which created from the 1st loop. however, this is not happening. What it happens is var17 moved on step down

 

Any help it would be appreciated 🙂 

 

Obs date var17 var18 var15
1 31/03/2000 . . .
2 30/06/2000 . . .
3 30/09/2000 . . .
4 31/12/2000 . . .
5 31/03/2001 . . .
6 30/06/2001 . . .
7 30/09/2001 . . .
8 31/12/2001 . . .
9 31/03/2002 . . .
10 30/06/2002 . . .
11 30/09/2002 . . .
12 31/12/2002 . . .
13 31/03/2003 . . .
14 30/06/2003 . . .
15 30/09/2003 . . .
16 31/12/2003 . . .
17 31/03/2004 . . .
18 30/06/2004 . . .
19 30/09/2004 . . .
20 31/12/2004 . . .
21 31/03/2005 . . .
22 30/06/2005 . . .
23 30/09/2005 -0.7979 0.07234 -0.41651
24 31/12/2005 -0.3274 -0.04446 0.10851
25 31/03/2006 0.0896 -0.01812 -0.28162
26 30/06/2006 -0.3205 0.02743 0.02995

 

 

 

%MACRO example(COUNT);
%DO I = 1 %TO &COUNT;
%DO X = 2 %TO &COUNT;

 

data data&x;
set data&i;
&var17=lag1(&var15);
run;

 

PROC SQL;

UPDATE data&x SET &var24=&var17*(-0.1487);
UPDATE data&x SET &var18=&var22 + &var23 + &var24 + d1_co + d2_co + d3_co;
UPDATE data&x SET &var6= &var18;
QUIT;

 

data data&x;
set data&x;
&var5=lag1(&var5)+ &var6;
&var15= &var5 - &var16;
run;

%end;
%END;
%MEND example;

 

%example(10);

4 REPLIES 4
Astounding
PROC Star

You are missing big pieces of the picture.  For example, you refer to &x15 and &x17 but never show where they come from, or any of the log messages.

 

It is entirely possible that this shouldn't be a macro problem at all.  Try showing what the data looks like now, and what you would like as the result.

Toni2
Lapis Lazuli | Level 10

thanks for your response. I found it now 🙂

Rick_SAS
SAS Super FREQ

I don't see a definition for the macro variables &var17, &var15, and so forth. Maybe remove the ampersands if those are the actual names of the variables?

 

The Fundamental Rule of Macro Programming is "a macro generates text; nothing less, nothing more." Of course, in practice, the text is often a SAS program. Therefore the Fundamental Rule of Macro Debugging is "write SAS code that works before you try to wrap it into a macro."  Start with the SAS code that is being generated for &i=1 and &x=1. If there is no error in that code, move on to &i=1 and &x=2. 

Toni2
Lapis Lazuli | Level 10

thanks for the response and the advise. I followed your advise and works now! Firstly, I created the code for &i=1 and &x=1 and when it run with no errors i added the macro. Thank you 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 750 views
  • 4 likes
  • 3 in conversation