DATA Step, Macro, Functions and more

Data "set" step not resolving inside a macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Data "set" step not resolving inside a macro

Hi all,

 

I am trying to write a macro where the first step creates some stats from a t-test, then I do some additional work on the ods output and refer to it by the macro deined "%let" name. I keep getting a "Error 180-322: Statement is not valid or it is used out of proper order" with by step statement in a data step. I'm at a loss.  Below is a snippet of the macro:

 

%macro m1 (varlist);

%local i next ;

%let i=1;

%do i=1 %to %sysfunc(countw(&varlist));
%let next=%scan(&varlist,&i);

* Step 1;
%let lib1 = work;
%let lib2 = work;
%let fileorg = survey_of_interest;
%let pre = &next;
%let survey = "&next";

DATA survey_of_interest;
SET data_raw_2;
WHERE survey_name = &survey;
RUN;

ods output "Statistics" = &pre.stats
"T-Tests" = &pre.ttests
"Equality of Variances" = &pre.vars;
proc ttest data=&lib2..&fileorg;
class finaid;
*by survey_name;
var _numeric_;
run;
ods output close;

*Step 2;
*%let lib1 = work;
*%let pre = finaid_css;
data &pre.stats1 (keep = survey_name variable class
mean_nonfinaid mean_finaid pooled_sd);
set work.&&pre.stats (rename=(mean=avg));
%if class = '0'
%then mean_nonfinaid = avg;
%if class = '1'
%then mean_finaid = avg;

%if class = 'Diff (1-2)'
%then pooled_sd = StdDev;
run;
%end;

%mend m1;

%m1(CSS);

 

I seem to have all the semicolons, but I don't know what is happening.


Accepted Solutions
Solution
‎01-19-2018 02:48 PM
Super User
Posts: 13,084

Re: Data "set" step not resolving inside a macro

Please look at this from your code:

data &pre.stats1 (keep = survey_name variable class
mean_nonfinaid mean_finaid pooled_sd);
set work.&&pre.stats

 

The last is going to be looking for a macro variable named something that I don't think exists.

 

If you run your code with

Options Mprint Symbolgen;

before executing the macro the log will likely give you more information about what you actually created.

Turn off with

Options Nomprint Nosymbolgen;

 

Also you used some comments here:

ods output close;

*Step 2;
*%let lib1 = work;
*%let pre = finaid_css;

 

Which may cause unexpected results.

Inside a macro block you want comments to be either

/* %let lib1=work; */

or

%* %let lib1 = work;

 

For a number of reasons I always use the /*  */ comment. One reason: editor support for comment /uncomment with Ctrl-/ and Shift-Ctrl-/

View solution in original post


All Replies
PROC Star
Posts: 1,357

Re: Data "set" step not resolving inside a macro

*Step 2;
*%let lib1 = work;
*%let pre = finaid_css;
data &pre.stats1 (keep = survey_name variable class
mean_nonfinaid mean_finaid pooled_sd);
set work.&&pre.stats (rename=(mean=avg));
%if class = '0'
%then mean_nonfinaid = avg;
%if class = '1'
%then mean_finaid = avg;

%if class = 'Diff (1-2)'
%then pooled_sd = StdDev;
run;
%end;

%mend m1;

%m1(CSS);

Assuming Class is a variable in the dataset specified in set statement, I do not get why you are using macro %if %then statements to evaluate datastep variables such as class, shouldn't it be just if then statements?

Solution
‎01-19-2018 02:48 PM
Super User
Posts: 13,084

Re: Data "set" step not resolving inside a macro

Please look at this from your code:

data &pre.stats1 (keep = survey_name variable class
mean_nonfinaid mean_finaid pooled_sd);
set work.&&pre.stats

 

The last is going to be looking for a macro variable named something that I don't think exists.

 

If you run your code with

Options Mprint Symbolgen;

before executing the macro the log will likely give you more information about what you actually created.

Turn off with

Options Nomprint Nosymbolgen;

 

Also you used some comments here:

ods output close;

*Step 2;
*%let lib1 = work;
*%let pre = finaid_css;

 

Which may cause unexpected results.

Inside a macro block you want comments to be either

/* %let lib1=work; */

or

%* %let lib1 = work;

 

For a number of reasons I always use the /*  */ comment. One reason: editor support for comment /uncomment with Ctrl-/ and Shift-Ctrl-/

Occasional Contributor
Posts: 8

Re: Data "set" step not resolving inside a macro

I killed the comments complete and the macro worked. I did not realise the funky behaviour with the comments with a single *;...The more you know!

 

Thanks everyone!

Super User
Posts: 13,084

Re: Data "set" step not resolving inside a macro


dewittme wrote:

I killed the comments complete and the macro worked. I did not realise the funky behaviour with the comments with a single *;...The more you know!

 

Thanks everyone!


It has been quite a few years ago but I know I spent hours going over some simple code that I had made one small change to that blew up. This was pretty much before most of the internet on line forums existed so there was a lot of trial and error involved. And reading the documentation. Once I had a clue where to look it was there but not the most obvious thing to chase down.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 133 views
  • 2 likes
  • 3 in conversation