<?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: macro change multiple variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378721#M276817</link>
    <description>&lt;P&gt;yes, I know what is happening ... each time you run the program, you create a new data set named WANT, which erases previous data sets named WANT, and so the last time you run the program, the variable in WANT is dissatisfyk.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can all be avoided by not using macros here, but instead using an ARRAY statement or two, something like (UNTESTED CODE)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;array q07 q07a q07b q07c /* I'm not going to type them all */ ;
array dis dissatisfya dissatisfyb dissatisfyc;
do I=1 to dim(q07);
     if q07(I)=4 or q07(I)=5 then dis(I)=1; else dis(I)=0;
end;&lt;/PRE&gt;</description>
    <pubDate>Mon, 24 Jul 2017 15:17:57 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2017-07-24T15:17:57Z</dc:date>
    <item>
      <title>macro change multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378716#M276815</link>
      <description>&lt;P&gt;Hi -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to write a macro for a if then command to create a series of dummy varibales, my code looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro change(var);
data want;
   set data;
   if Q07&amp;amp;var. = 4 or Q07&amp;amp;var. = 5 then dissatisfy&amp;amp;var. = 1;
   else dissatisfy&amp;amp;var. = 0;
run;
%mend;
%change(f);
%change(h);
%change(b);
%change(a);
%change(e);
%change(i);
%change(d);
%change(g);
%change(c);
%change(j);
%change(k);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The ideal new dummy variables woule be: dissatisfyf dissatisfyh dissatisfyb....dissatisfyk. However, when I runned this, I could just get dissatisfyk.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know what's wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!!!&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2017 15:08:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378716#M276815</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2017-07-24T15:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: macro change multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378718#M276816</link>
      <description>&lt;P&gt;You always start from the same data set. In this case, your previous runs are not saved, only the last run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;; *-&amp;gt; Created new each time from data - previous data not kept;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; Q07&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;4&lt;/SPAN&gt; or Q07&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;5&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; dissatisfy&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;else&lt;/SPAN&gt; dissatisfy&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Keep the dataset name the same or instead, use an array may simplify this a lot. &amp;nbsp;Other option is to use a format. &amp;nbsp;I would strongly recommend looking at the automated ways of creating dummy variables such as using GLMMOD. And depending on the PROC, it may handle it already, these procs would include Logistic, GLM, PHREG.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;; *-&amp;gt; Created new each time from data - previous data not kept;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;want&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; Q07&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;4&lt;/SPAN&gt; or Q07&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;5&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; dissatisfy&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;else&lt;/SPAN&gt; dissatisfy&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jul 2017 15:12:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378718#M276816</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-24T15:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: macro change multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378721#M276817</link>
      <description>&lt;P&gt;yes, I know what is happening ... each time you run the program, you create a new data set named WANT, which erases previous data sets named WANT, and so the last time you run the program, the variable in WANT is dissatisfyk.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can all be avoided by not using macros here, but instead using an ARRAY statement or two, something like (UNTESTED CODE)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;array q07 q07a q07b q07c /* I'm not going to type them all */ ;
array dis dissatisfya dissatisfyb dissatisfyc;
do I=1 to dim(q07);
     if q07(I)=4 or q07(I)=5 then dis(I)=1; else dis(I)=0;
end;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jul 2017 15:17:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378721#M276817</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-07-24T15:17:57Z</dc:date>
    </item>
    <item>
      <title>Re: macro change multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378722#M276818</link>
      <description>&lt;P&gt;1. your code creates the same data set for each macro call&lt;/P&gt;&lt;P&gt;that's why you get the results of only the last call.&lt;/P&gt;&lt;P&gt;Check your log to see:&lt;/P&gt;&lt;P&gt;DATA want;&lt;/P&gt;&lt;P&gt;q07a = ...;&lt;/P&gt;&lt;P&gt;DATA want;&lt;/P&gt;&lt;P&gt;q07b = ...;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. macro&amp;nbsp; is not necessary here&lt;/P&gt;&lt;P&gt;learn how to use arrays&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;DATA want;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;array _q (*) q07a -- q07h;*may have to list q07a q07b q07c ....;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;array _d(*) disa -- dish;*names must align with above;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;set data;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;do _i = 1 to dim(_q);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; _d(_i) = (_d(_i)&amp;nbsp; eq 4&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; or _d(_i)&amp;nbsp; eq 5);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ron Fehd not always macro-needed maven&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2017 15:21:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378722#M276818</guid>
      <dc:creator>Ron_MacroMaven</dc:creator>
      <dc:date>2017-07-24T15:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: macro change multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378726#M276819</link>
      <description>&lt;P&gt;Just turn on the MPRINT option and look at the generated code. You will see that you keep overwriting the target dataset so only the last call has any effect. You have basically put your macro loop in the wrong place. You want to loop INSIDE of a single data step to create multiple variables in that data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you really don't need macro code for this problem. &amp;nbsp;Just use arrays.&lt;/P&gt;
&lt;P&gt;Let's call your input dataset HAVE instead of DATA to avoid confusion the the DATA statement. &amp;nbsp;Also let's make your new variable names using numeric suffix instead of letter suffixes to save a lot of typing.&lt;/P&gt;
&lt;P&gt;Also we can take advantage of how SAS evaluates boolean expressions to 1 or 0 to simplify the code by replacing the IF/THEN/ELSE construct with a simple assignment statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   array old Q07a Q07b Q07c Q07d Q07e Q07f Q07g Q07h Q07i Q07j Q07k;
   array new dissatisfy1 - dissatisfy11 ;
   do i=1 to dim(old);
     new(i) = old(i) in (4,5);
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jul 2017 15:28:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-change-multiple-variables/m-p/378726#M276819</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-24T15:28:25Z</dc:date>
    </item>
  </channel>
</rss>

