<?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: Loop Through Variable List to Create new variables with Macro in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562320#M10720</link>
    <description>&lt;P&gt;use a data step and do an append to add variable's to the original dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 May 2019 15:31:57 GMT</pubDate>
    <dc:creator>VDD</dc:creator>
    <dc:date>2019-05-29T15:31:57Z</dc:date>
    <item>
      <title>Loop Through Variable List to Create new variables with Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562318#M10719</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It feels like it has been a while since I came for your sage advice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a macro that looks like the following:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro yes(Exp);
data alldata_JB_052819;
set sub.alldata_jb_020819;
 PreResYr2nd6mo_&amp;amp;Exp =  PreResYr2nd6mo_&amp;amp;Exp + 1;
PostResYr1st6mo_&amp;amp;Exp = PostResYr1st6mo_&amp;amp;Exp + 1;
Prop&amp;amp;Exp = PostResYr1st6mo_&amp;amp;Exp/PreResYr2nd6mo_&amp;amp;Exp;
run;
%mend;
%yes(House);
%yes(Consume);
%yes(Durable);
%yes(Trans);
%yes(Health);
%yes(Insure);
%yes(Loan);
%yes(Tax);
%yes(Educa);
%yes(Gift);
%yes(Enter);
%yes(Recre);
%yes(FinPlan);
%yes(Legala);
%yes(AlcSpend);
%yes(ST2DENOM2);
%yes(ST2DENOM3);
%yes(ExpendTL);
%yes(FinPlSv);
%yes(IncomeTL);
%yes(ST2Oblig);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This macro is looping through each of these variables and then only creating the last variable in the new dataset. In this case, it is PropST2Oblig.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I make this macro loop through each of these variables and create a new variable (proportion of Post/Pre) for each instance?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For reference, the data set is quite large and in wide format. I want to create a proportion for each of the variables at each of the time intervals ( e.g., 12 1-month prop, 4 quarter-prop, 3 trimester-prop, etc.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am sure I could reduce the input somehow, but came looking for advice/what problem my macro has&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 15:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562318#M10719</guid>
      <dc:creator>joebacon</dc:creator>
      <dc:date>2019-05-29T15:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loop Through Variable List to Create new variables with Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562320#M10720</link>
      <description>&lt;P&gt;use a data step and do an append to add variable's to the original dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 15:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562320#M10720</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-05-29T15:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: Loop Through Variable List to Create new variables with Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562322#M10721</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro yes(Exp);
data temp;
set sub.alldata_jb_020819;
 PreResYr2nd6mo_&amp;amp;Exp =  PreResYr2nd6mo_&amp;amp;Exp + 1;
PostResYr1st6mo_&amp;amp;Exp = PostResYr1st6mo_&amp;amp;Exp + 1;
Prop&amp;amp;Exp = PostResYr1st6mo_&amp;amp;Exp/PreResYr2nd6mo_&amp;amp;Exp;
run;
proc append base=temp new=all;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is a simple way of getting everything into one data set, which I have cleverly named ALL., but you get a lot of missing values this way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Probably a better way, which avoids the missing values problem:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro yes(Exp);
data alldata_JB_052819;
set sub.alldata_jb_020819;
%do i=1 %to %sysfunc(countw(&amp;amp;exp));
    %let thisexp=%scan(&amp;amp;exp,&amp;amp;i,%str( ));
 PreResYr2nd6mo_&amp;amp;thisExp =  PreResYr2nd6mo_&amp;amp;thisExp + 1;
PostResYr1st6mo_&amp;amp;thisExp = PostResYr1st6mo_&amp;amp;thisExp + 1;
Prop&amp;amp;thisExp = PostResYr1st6mo_&amp;amp;thisExp/PreResYr2nd6mo_&amp;amp;thisExp;
run;
%mend;
%yes(house consume durable) /* You type the rest of the variable names, I'm too lazy */&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 May 2019 15:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562322#M10721</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-29T15:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: Loop Through Variable List to Create new variables with Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562325#M10723</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;You crack me up. Thank you for taking the time to help me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I attempted to implement the second solution, however, it is throwing the error: "ERROR 180-322: Statement is not valid or it is used out of proper order." when looping through the list of variables. House got inputted, but none of the other variables did.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the slightly modified code to include an "end" statement to the do-Loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro yes(Exp);
data alldata_JB_052819;
set sub.alldata_jb_020819;
%do i=1 %to %sysfunc(countw(&amp;amp;exp));
    %let thisexp=%scan(&amp;amp;exp,&amp;amp;i,%str( ));
 PreResYr2nd6mo_&amp;amp;thisExp =  PreResYr2nd6mo_&amp;amp;thisExp + 1;
PostResYr1st6mo_&amp;amp;thisExp = PostResYr1st6mo_&amp;amp;thisExp + 1;
Prop&amp;amp;thisExp = PostResYr1st6mo_&amp;amp;thisExp/PreResYr2nd6mo_&amp;amp;thisExp;
run;
%end;
%mend;
%yes(house consume durable Trans health Insure Loan Tax Educa Gift Enter REcre FinPlan Legala AlcSpend ST2DENOM2 ST2DENOM3 ExpendTl FinPlSv IncomeTL STOblig);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any recommendations?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 15:51:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562325#M10723</guid>
      <dc:creator>joebacon</dc:creator>
      <dc:date>2019-05-29T15:51:43Z</dc:date>
    </item>
    <item>
      <title>Re: Loop Through Variable List to Create new variables with Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562339#M10729</link>
      <description>Move the RUN; statement to after the %END; statement.</description>
      <pubDate>Wed, 29 May 2019 16:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562339#M10729</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-29T16:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: Loop Through Variable List to Create new variables with Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562347#M10731</link>
      <description>Good catch. Silly mistake. Thank you. I must say that was rather Astounding...</description>
      <pubDate>Wed, 29 May 2019 16:51:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loop-Through-Variable-List-to-Create-new-variables-with-Macro/m-p/562347#M10731</guid>
      <dc:creator>joebacon</dc:creator>
      <dc:date>2019-05-29T16:51:04Z</dc:date>
    </item>
  </channel>
</rss>

