DATA Step, Macro, Functions and more

An error when using % macro statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

An error when using % macro statement

Dear community,

 

I am trying to run the following macro, a lot of errrors result. I

 

%macro unemp4(ls);
DATA  asmt2.gdp3;
set asm2.gdp
L&ls. = LOG(GDP) - LOG (LAG&ls.(GDP));
S&ls. = LAG&ls.(SPREAD);

PROC  REG  DATA=asmt2.gdp3;
    MODEL  L&ls.=S&ls./DWPROB;
RUN;
quit;
%MEND unemp4;
%unemp4 (1) %unemp4(2) %unemp4 (3) %unemp4 (4)  %unemp4(5) %unemp4 (6)
%unemp4(7) %unemp4 (8)

 

Thank you for your help!


Accepted Solutions
Solution
‎12-08-2015 10:50 AM
Super User
Posts: 19,782

Re: An error when using % macro statement

You're overwriting your data set with each call. So the first call creates data set gdp3 and then the second call creates a new gdp3 data set, with the source data gdp and everything from the first step is forgotten. You may want to look into a macro loop in your data step instead of multiple calls, as well as a way to store your final data set.

View solution in original post


All Replies
Super User
Posts: 11,343

Re: An error when using % macro statement

It would help if you run some of the code with OPTIONS MPRINT; and show the log so we know what type of errors you are getting if syntax errors.

 

If you are getting unexpected value errors you should provide some example input, the erroneous values and the desired values.

Occasional Contributor
Posts: 12

Re: An error when using % macro statement

I have attached the log.

 

What I am trying to do is instead of writing this long code:

 

DATA  asmt2.gdp3;
    SET  asmt2.gdp;
    L1=LOG(GDP)-LOG(LAG1(GDP));
    L2=LOG(GDP)-LOG(LAG2(GDP));
    L3=LOG(GDP)-LOG(LAG3(GDP));
    L4=LOG(GDP)-LOG(LAG4(GDP));
    L5=LOG(GDP)-LOG(LAG5(GDP));
    L6=LOG(GDP)-LOG(LAG6(GDP));
    L7=LOG(GDP)-LOG(LAG7(GDP));
    L8=LOG(GDP)-LOG(LAG8(GDP));
    S1=LAG1(SPREAD);
    S2=LAG2(SPREAD);
    S3=LAG3(SPREAD);
    S4=LAG4(SPREAD);
    S5=LAG5(SPREAD);
    S6=LAG6(SPREAD);
    S7=LAG7(SPREAD);
    S8=LAG8(SPREAD);
RUN;

 

I am trying to write a macro version:

 

%macro unemp4(ls);
DATA  asmt2.gdp3;
set asm2.gdp
L&ls. = LOG(GDP) - LOG (LAG&ls.(GDP));
S&ls. = LAG&ls.(SPREAD);
RUN;
quit;
%MEND unemp4;
%unemp4 (1) %unemp4(2) %unemp4 (3) %unemp4 (4)  %unemp4(5) %unemp4 (6)
%unemp4(7) %unemp4 (8)

Trusted Advisor
Posts: 1,117

Re: An error when using % macro statement

Adding the missing semicolon after the SET statement will help a lot, I guess.

Solution
‎12-08-2015 10:50 AM
Super User
Posts: 19,782

Re: An error when using % macro statement

You're overwriting your data set with each call. So the first call creates data set gdp3 and then the second call creates a new gdp3 data set, with the source data gdp and everything from the first step is forgotten. You may want to look into a macro loop in your data step instead of multiple calls, as well as a way to store your final data set.
Occasional Contributor
Posts: 12

Re: An error when using % macro statement

Thanks, I see the issue. I will try a DO loop
Super User
Posts: 19,782

Re: An error when using % macro statement

If you have proc expand, or a SAS/ETS license this is also a lot easier to calculate. It's worth looking into.
Super User
Super User
Posts: 7,942

Re: An error when using % macro statement

Hi,


Well, firstly, what are the errors you get in the log.  You have provided no log, nor test data to run this against.  Its not quite clear what your macro is attempting to do, or why all the macro code is necessary.  Also note that code formatting is quite important to be clearly readble.  One important thing to note, which will cause errors is the missing semicolon after each macro call, update as given below.  Also, be sure to put options mlogic mprint symbolgen; on before running.

%macro unemp4(ls);
  data asmt2.gdp3; 
    set asm2.gdp
    L&ls.=log(gdp) - log (lag&ls.(gdp));
    S&ls.=lag&ls.(spread);
  run;

  proc reg data=asmt2.gdp3;
    model l&ls.=s&ls./dwprob; 
  run;
quit;
%mend unemp4;
%unemp4 (1);
%unemp4 (2);
%unemp4 (3); 
%unemp4 (4);  
%unemp4 (5); 
%unemp4 (6); 
%unemp4 (7); 
%unemp4 (8);

 

Contributor ndp
Contributor
Posts: 61

Re: An error when using % macro statement

Assuming GDP and SPREAD are variables in the dataset i believe error is from use LAG1()-LAG8() functions as these are not valid sas functions.

Super User
Posts: 19,782

Re: An error when using % macro statement

Contributor ndp
Contributor
Posts: 61

Re: An error when using % macro statement

@Reeza Thanks should have checked before i responded.

Occasional Contributor
Posts: 12

Re: An error when using % macro statement

Thanks for your help ndp
🔒 This topic is solved and locked.

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

Discussion stats
  • 11 replies
  • 749 views
  • 1 like
  • 6 in conversation