How to apply macro to data step and proc reg

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 130
Accepted Solution

How to apply macro to data step and proc reg

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';

Attachment

Accepted Solutions
Solution
‎04-12-2014 11:42 PM
Super User
Posts: 9,671

Re: How to apply macro to data step and proc 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

View solution in original post


All Replies
Solution
‎04-12-2014 11:42 PM
Super User
Posts: 9,671

Re: How to apply macro to data step and proc 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

Frequent Contributor
Posts: 130

Re: How to apply macro to data step and proc reg

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.

Super User
Posts: 9,671

Re: How to apply macro to data step and proc reg

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

Post your ERROR log.

Frequent Contributor
Posts: 130

Re: How to apply macro to data step and proc reg

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!

☑ This topic is SOLVED.

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

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