<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Need help with automated macro code for creating two-way interaction variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238012#M43682</link>
    <description>Ok, start with the allcomb function instead of manually creating combinations.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html" target="_blank"&gt;http://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html&lt;/A&gt;</description>
    <pubDate>Sun, 06 Dec 2015 14:18:22 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2015-12-06T14:18:22Z</dc:date>
    <item>
      <title>Need help with automated macro code for creating two-way interaction variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/237995#M43677</link>
      <description>&lt;P&gt;Working with the following toy dataset, I'm trying to create code which will automatically calculate two-way interaction variables for a given set of main effects named "black", "white" and "red".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input black white red;
cards;
10 20 30
40 50 60
70 80 90
;
run;

proc contents data=test out=varlist(keep=name);
run;

data _null_;
length allvars $20;
retain allvars ' ';
set varlist end=eof;
allvars = trim(left(allvars))||' '||left(name);
if eof then call symput('varlist', allvars);
run;
%put &amp;amp;varlist;
/*
black red white
*/

%macro create_interaction_vars();
	data test;
	set test;
		%let separator_s =%str( );
		%do i=1 %to 3;
			%let v&amp;amp;i = %scan(&amp;amp;varlist, &amp;amp;i, %str( ));
		%end;
		%do i=1 %to 3;
			%do j=1 %to 3;
				%if &amp;amp;i &amp;lt;= &amp;amp;j %then %goto continue; 
					data test;
					set test;
						w&amp;amp;i.&amp;amp;j = &amp;amp;&amp;amp;v&amp;amp;i.*&amp;amp;&amp;amp;v&amp;amp;j;
						rename w&amp;amp;i.&amp;amp;j = "&amp;amp;v&amp;amp;i._&amp;amp;v&amp;amp;j_int";
					run;
					%continue:
			%end;
		%end;
	run;
%mend;
%create_interaction_vars();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have no problems creating interactions variables named w12, w13, and w23. However, I'd like the interaction terms to have&amp;nbsp;more informative&amp;nbsp;names created from the main effect terms such as:&amp;nbsp;"white_red_int", "white_black_int", and "red_black_int", but am getting tied up when it comes to concatenating two macro variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is much appreciated, thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Robert&lt;/P&gt;</description>
      <pubDate>Sun, 06 Dec 2015 03:53:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/237995#M43677</guid>
      <dc:creator>RobF</dc:creator>
      <dc:date>2015-12-06T03:53:04Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with automated macro code for creating two-way interaction variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/237997#M43678</link>
      <description>&lt;P&gt;Are you familiar with the SAS shortcut notation for creating two way interactions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the | with&amp;nbsp;@N to create your two way interactions. I can't find the documentation but I'm sure it's in there somewhere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example using SASHELP.CARS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc glm data=sashelp.cars;
model weight = invoice | length | mpg_city | mpg_highway @2;
run;
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 06 Dec 2015 04:40:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/237997#M43678</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-06T04:40:35Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with automated macro code for creating two-way interaction variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238011#M43681</link>
      <description>&lt;P&gt;Reeza -&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I've used that feature and it's quite handy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case I have a lot of variables &amp;amp; the dataset will be exported to a text file for analysis in other programs. Otherwise I'd use the | and n@ option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Robert&lt;/P&gt;</description>
      <pubDate>Sun, 06 Dec 2015 14:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238011#M43681</guid>
      <dc:creator>RobF</dc:creator>
      <dc:date>2015-12-06T14:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with automated macro code for creating two-way interaction variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238012#M43682</link>
      <description>Ok, start with the allcomb function instead of manually creating combinations.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html" target="_blank"&gt;http://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html&lt;/A&gt;</description>
      <pubDate>Sun, 06 Dec 2015 14:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238012#M43682</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-06T14:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with automated macro code for creating two-way interaction variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238022#M43683</link>
      <description>&lt;P&gt;You can build on&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;'s suggestion with proc GLMMOD and get your design matrix directly into a dataset. Just add your dependent variable to your dataset :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id green black white red;
cards;
1 2.1 10 20 30
2 3.2 40 50 60
3 4.3 70 80 90
;

proc glmmod data=test outdesign=testd outparm=testp;
model green = black | white | red @2 / noint;
weight id; /* Just copy this variable */
run;

proc sql;
select 
    cats("Col", _COLNUM_, "=", translate(EFFNAME, "_", "*")),
    cats("Col", _COLNUM_, "=""", EFFNAME, """")
    into :renameVars separated by " ", :labelVars separated by " "
from testp;
quit;

data myDesign;
set testd;
rename &amp;amp;renameVars;
label &amp;amp;labelVars;
run;

proc print data=myDesign noobs label; run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 06 Dec 2015 20:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238022#M43683</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-12-06T20:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with automated macro code for creating two-way interaction variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238075#M43705</link>
      <description>&lt;P&gt;Thanks PGStats, this works great!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There must still be a way to combine two macro variable names like "black" and "white" into a single varibale name "black_white_int", but getting the macro code syntax right with the ampersands and quotation marks has always been tricky for me.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Dec 2015 15:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238075#M43705</guid>
      <dc:creator>RobF</dc:creator>
      <dc:date>2015-12-07T15:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with automated macro code for creating two-way interaction variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238182#M43730</link>
      <description>&lt;P&gt;Here's another solution to creating interaction variables that avoids macro programming.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create a variable list of the main effects of interest from proc contents, then accumulate all of the interaction variable creation data step statements into one long macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, to create the interaction variables green_red_int, black_red_int, and white_red_int, run the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id green black white red;
cards;
1 2.1 10 20 30
2 3.2 40 50 60
3 4.3 70 80 90
;

proc contents data=test out=varlist(keep=varnum name) order=varnum;
run;
proc sort data=varlist;
	by varnum;
run;
data varlist;
set varlist;
	if varnum in(2:4);
run;

data _null_;
length allvars $90;
retain allvars;
set varlist end=eof;
	allvars =trim(left(allvars))||' '||trim(name)||'_red_int = '||trim(name)||'*red;';
	if eof then call symput('interactions', allvars);
run;
%put &amp;amp;interactions;

data test2;
set test;
	&amp;amp;interactions;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Dec 2015 22:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-automated-macro-code-for-creating-two-way/m-p/238182#M43730</guid>
      <dc:creator>RobF</dc:creator>
      <dc:date>2015-12-07T22:13:19Z</dc:date>
    </item>
  </channel>
</rss>

