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

I wrote the following macro, but the output contains no data. I believe there are syntax problems in the first and second steps.

For the following macro:

1) if I delete step 2 and step 3 code, then running the macro only create 3 empty data sets;

2) if I first manually create 3 data sets, then run the macro (after deleting step 1 and step 3 code), the macro's outputs still contain no results;

3) if I manually do the step 1 and step 2, then run the macro (after deleting step 1 and step 2 code), now I get the excel file which contains the results I want.

%macro DiDreg;

%do i = 1 %to 3;

/*step 1, create 3 new datasets (want_1, want_2, want_3) from originaldata;*/

data want_&i;

  set originaldata;

  if a_&i=2 then delete;

  c_&i = a_&i * b;

run;

/*step 2, run regression by using want_1, want_2, want_3;*/

proc reg data=want_&i outest = did_&i tableout noprint;

model y = x b a_&i c_&i;

run;

/*step 3, export to excel;*/

proc export dbms = excel

     outfile = "C:\folder\DiD_reg.xls"

     data=did_&i replace;

     sheet = "did_&i";

run;

%end;

%mend;

%DiDreg;

***********************************************************;

Desired Results:

*step 1, create three datasets;

*want_1 contains variable: y, x, b, c_1, a_1, a_2, a_3;

*want_2 contains variable: y, x, b, c_2, a_1, a_2, a_3;

*want_3 contains variable: y, x, b, c_3, a_1, a_2, a_3;

*step 2, run three regressions;

*regession 1: y x b a_1 c_1, and output to did_1;

*regession 2: y x b a_2 c_2, and output to did_2;

*regession 3: y x b a_3 c_3, and output to did_3;

*step 3, combine did_1, did_2 and did_3 to excel file 'did_reg';

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Your code looks good except:

*step 1, create 3 new datasets (want_1, want_2, want_3) from originaldata;


==>


/*step 1, create 3 new datasets (want_1, want_2, want_3) from originaldata;*/


use /* */ in macro not * ; as comment

View solution in original post

4 REPLIES 4
Ksharp
Super User

Your code looks good except:

*step 1, create 3 new datasets (want_1, want_2, want_3) from originaldata;


==>


/*step 1, create 3 new datasets (want_1, want_2, want_3) from originaldata;*/


use /* */ in macro not * ; as comment

Jonate_H
Quartz | Level 8

thank you for point that out, I have changed in my question. those comments didn't include in my code when I running it though.

Ksharp
Super User

That is weird . I can run your code without error.

Post your ERROR log.

Jonate_H
Quartz | Level 8

Thank you Ksharp. Just found that I also can run my code without error (using the sample data set I created).... Then it must be the problem of my working data set!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1552 views
  • 0 likes
  • 2 in conversation