<?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 list of variables to loop through proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750422#M236046</link>
    <description>&lt;P&gt;Agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;as well. Although I posted macro code, the better approach is to have a smarter arrangement of your data (long, not wide), which then makes coding a lot simpler.&lt;/P&gt;</description>
    <pubDate>Fri, 25 Jun 2021 12:24:30 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-06-25T12:24:30Z</dc:date>
    <item>
      <title>Macro list of variables to loop through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750264#M235947</link>
      <description>&lt;P&gt;Hello folks, I have been searching for answers to this but I can't find the best solution so far. I want to write a macro to loop through a lot of variables in a proc SQL join command. Below is my code with one of the variables in the dataset:&lt;/P&gt;&lt;PRE&gt;proc sql;
create table z1 as
select A.svscd, 
	   A.ufs_mo,
	   B.fym201703
	from Z A
	left join (select distinct svscd, fym201703 from Z where fym201703 ne .) B
	on 	A.svscd=B.svscd;
quit;&lt;/PRE&gt;&lt;P&gt;the variable "fym201703" is the one I need to create a loop so that the above code can be applied to all variables start with "fym" and it goes by month from 201703 to 202106. Therefore, I need to create multiple datasets like "z1" above for each of the fym variable I am looping through. I don't want to write down all the variables in the code, is there a way to handle this? Thanks a lot!!!&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jun 2021 17:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750264#M235947</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2021-06-24T17:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro list of variables to loop through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750269#M235950</link>
      <description>&lt;P&gt;UNTESTED CODE since we don't have your data sets&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dothis;
    %let startmonth=%sysevalf('01MAR2017'd);
    %let endmonth=%sysevalf('01JUN2021'd);
    %let month=&amp;amp;startmonth;
    %let month1=%sysfunc(putn(&amp;amp;month,yymm6.));
    %let incr=0;
    %do while(&amp;amp;month&amp;lt;=&amp;amp;endmonth);
         /* your sql goes here using variable &amp;amp;month1 instead of fym201703 */
         %let incr=%eval(&amp;amp;incr+1);
         %let month=%sysfunc(intnx(month,&amp;amp;month,&amp;amp;incr,b));
         %let month1=%sysfunc(putn(&amp;amp;month,yymm6.));
    %end;
%mend dothis;
         
    &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Jun 2021 17:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750269#M235950</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-24T17:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: Macro list of variables to loop through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750375#M236025</link>
      <description>&lt;P&gt;The code you posted is not creating the variable FYM201703, it is just referencing it.&lt;/P&gt;
&lt;P&gt;I suspect that you meant that you need to macro code to calculate the NAME of the variable you want to reference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To loop over dates use a %DO loop with an integer index and use INTNX() function to calculate the next date in the series.&lt;/P&gt;
&lt;P&gt;So if you want to months from March&amp;nbsp;&lt;SPAN&gt;2017 to June 2021 then use a %DO loop like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start='01MAR2017'd;
%let end='01JUN2021'd;

proc sql;
%do offset=0 to %sysfunc(intck(month,&amp;amp;start,&amp;amp;end));
  %let varname=FYM%sysfunc(intnx(month,&amp;amp;start,&amp;amp;offset),YYMMN6.);

create table z&amp;amp;offset as
  select A.svscd
       , A.ufs_mo
       , B.&amp;amp;varname
 from Z A
 left join (select distinct svscd, &amp;amp;varname from Z where not missing(&amp;amp;varname)) B
   on A.svscd=B.svscd
;

%end;

quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you only have the start and end dates in your YYYYMM 6 digit strings then use INPUTN() function to convert them into real dates to drive the INTCK() and INTNX() function calls.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jun 2021 04:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750375#M236025</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-25T04:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro list of variables to loop through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750378#M236027</link>
      <description>&lt;P&gt;Having variables named like "fym201703" looks like bad data-design, so i suggest to transpose the data moving the year/month information into a variable, because that's where it belongs. Then drop all obs with missing value and you will have a clear and easy to use dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;</description>
      <pubDate>Fri, 25 Jun 2021 04:38:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750378#M236027</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-06-25T04:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro list of variables to loop through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750380#M236028</link>
      <description>&lt;P&gt;What &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;said. This looks like a case for transposing first, so the "period" becomes data.&lt;/P&gt;
&lt;P&gt;Please provide a portion of your dataset in usable form (as a data step with datalines), so we can see what your code is intended to do for a single variable.&lt;/P&gt;
&lt;P&gt;In particular we need to know how many observations per svscd exist, and what values are possible in the fym... variables.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jun 2021 08:49:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750380#M236028</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-25T08:49:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro list of variables to loop through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750422#M236046</link>
      <description>&lt;P&gt;Agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;as well. Although I posted macro code, the better approach is to have a smarter arrangement of your data (long, not wide), which then makes coding a lot simpler.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jun 2021 12:24:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-list-of-variables-to-loop-through-proc-sql/m-p/750422#M236046</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-25T12:24:30Z</dc:date>
    </item>
  </channel>
</rss>

