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';
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
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
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.
That is weird . I can run your code without error.
Post your ERROR log.
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!
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.
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.
Ready to level-up your skills? Choose your own adventure.