Programming the statistical procedures from SAS

Creating interaction variable w/ macro

Reply
Occasional Contributor
Posts: 10

Creating interaction variable w/ macro

Hello,

I am trying to create interaction variables for a regression between 4 race dummy variables and several age category variables. My plan was to create a new variable name as a combo of the two variables used and then multiple the variables together creating the interaction variable. I've written the macro as follows:

%macro interaction(var);

data two; set one;
minority&var=minority*&var;
black_nonhisp&var = black_nonhisp*&var;
Asian_nonhisp&var = Asian_nonhisp &var;
other_nonhisp&var=other_nonhisp&var;

%mend interaction;

%interaction (_18to21);
%interaction (_22to24);
%interaction (_25to29);
%interaction (_30to34);
%interaction (_35to39);
%interaction (_40to44);
%interaction (_45to49);
%interaction (_50to54);
%interaction (_55to60);

run;

When I run the program I am getting errors (below) and was hoping for a little debugging help as this is one of my first times to write a macro.

Thanks!
21061 %interaction (_18to21);
NOTE: Line generated by the macro variable "VAR".
1 _18to21
-------
22
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL,
NOTIN, OR, ^=, |, ||, ~=.
Occasional Contributor
Posts: 10

Re: Creating interaction variable w/ macro

Just to clarify, both variables are dummies.
Grand Advisor
Posts: 10,043

Re: Creating interaction variable w/ macro

Looks like the lines:
Asian_nonhisp&var = Asian_nonhisp &var;
other_nonhisp&var=other_nonhisp&var;


maybe should be
Asian_nonhisp&var = Asian_nonhisp * &var;
other_nonhisp&var=other_nonhisp *&var;

in the macro definition.
Occasional Contributor
Posts: 10

Re: Creating interaction variable w/ macro

Thanks! Your suggestions, along with moving the macro out of the original dataset and into a new one, did the trick. I appreciate you taking the time to read through my code!
Ask a Question
Discussion stats
  • 3 replies
  • 703 views
  • 0 likes
  • 2 in conversation