<?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: Do loop variable is not sequential in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371669#M88798</link>
    <description>&lt;P&gt;Oops.&amp;nbsp; I didn't transcribe my program to the forum properly.&amp;nbsp; Instead of&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %if &amp;amp;YYnextYY=&amp;amp;last_YYnextYY %then union;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it should have been&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %if &amp;amp;YYnextYY&amp;nbsp; NE &amp;amp;last_YYnextYY %then union;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will edit the original note.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Jun 2017 13:00:51 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-06-29T13:00:51Z</dc:date>
    <item>
      <title>Do loop variable is not sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371503#M88755</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I've many years of data I need to combine using SQL specifically. The data names are not in numerical order (e.g. 2011, 2012, 2013.. 2016.). Instead its in the year format of 1112, 1213, 1314, ....1516, 1617. My first year of data in the example is 2013 and last year of data is 2016.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the 4 datsets and a code that I'd spell out for each year. The do loop marcro&amp;nbsp;I wrote didn't work. Can you&amp;nbsp;point the errors for me? Much appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data havedata1314Q4; input id date;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2005&lt;BR /&gt;2 2006&lt;BR /&gt;;&lt;BR /&gt;data havedata1415Q4; input id date;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2005&lt;BR /&gt;4 2010&lt;BR /&gt;;data havedata1516Q4; input id date;&lt;BR /&gt;datalines;&lt;BR /&gt;2 2006&lt;BR /&gt;6 2012&lt;BR /&gt;;data havedata1617Q4; input id date;&lt;BR /&gt;datalines;&lt;BR /&gt;7 2013&lt;BR /&gt;8 2015&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*Code 1 - spelling out steps;&lt;BR /&gt;%let lastyr2=16; *16 as latest year of data - 2016;&lt;BR /&gt;%let lastyr3= %sysfunc(putn(&amp;amp;lastyr2,z2.))%sysfunc(putn(%eval(&amp;amp;lastyr2+1),z2.)); *eg yr=1617;&lt;BR /&gt;proc sql; create table combined as&lt;BR /&gt; select distinct ID, Date from HaveData1314Q4&lt;BR /&gt; union &lt;BR /&gt; select distinct ID, Date from HaveData1415Q4&lt;BR /&gt; union &lt;BR /&gt; select distinct ID, Date from HaveData1516Q4&lt;BR /&gt; union &lt;BR /&gt; select distinct ID, Date from HaveData&amp;amp;lastyr3.Q4;&lt;BR /&gt; create table want as &lt;BR /&gt; select distinct ID, date&lt;BR /&gt; from combined;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*Code 2 using do loop;&lt;BR /&gt;%let lastyr2=16; *16 as latest year of data - 2016;&lt;BR /&gt;%let lastyr3= %sysfunc(putn(&amp;amp;lastyr2,z2.))%sysfunc(putn(%eval(&amp;amp;lastyr2+1),z2.)); *eg yr=1617;&lt;BR /&gt;%let prevyr=20%sysfunc(putn(%eval(&amp;amp;lastyr2-1),z2.)); *get prevoius year 2015;&lt;BR /&gt;%macro get;&lt;BR /&gt;proc sql; create table out1 as&lt;BR /&gt;%do year=2013 %to &amp;amp;prevyr; &lt;BR /&gt;%let year1= %sysfunc(putn(&amp;amp;year.,z2.))%sysfunc(putn(%eval(&amp;amp;year.+1),z2.)); &lt;BR /&gt; select distinct ID, date from HaveData&amp;amp;year1.Q4 &lt;BR /&gt; union &lt;BR /&gt; %end;&lt;BR /&gt; select distinct ID, date from HaveData&amp;amp;lastyr3.Q4&lt;BR /&gt; create table want as &lt;BR /&gt; select distinct ID, date&lt;BR /&gt; from combined;&lt;BR /&gt;quit;&lt;BR /&gt;%mend get;&lt;BR /&gt;%get;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 01:07:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371503#M88755</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2017-06-29T01:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop variable is not sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371507#M88757</link>
      <description>&lt;P&gt;I'd suggest&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Make your upper level=1617,&amp;nbsp; not 16.&lt;/LI&gt;
&lt;LI&gt;make your loop index easier through use of the %BY clause, (i.e. %by 101 as below)&amp;nbsp; &amp;lt;i.e. 1314,1415,1516,...&amp;gt;,&lt;/LI&gt;
&lt;LI&gt;Inside the loop, append a UNION to every iteration but the last&lt;/LI&gt;
&lt;LI&gt;I think you can drop the DISTINCT modifier, because UNION apparently will remove duplicates&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let last_YYnextYY=1617;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;create table want as&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %do YYnextYY = 1213 %to &amp;amp;last_YYnextYY %by 101;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; select id, date from have&amp;amp;yynextYY.Q4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;YYnextYY NE &amp;amp;last_YYnextYY %then union;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 13:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371507#M88757</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-06-29T13:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop variable is not sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371509#M88758</link>
      <description>&lt;P&gt;First error: &amp;nbsp;expecting us to understand what you mean when you say it didn't work. &amp;nbsp;You must have gotten some sort of error message that you could share.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of trying to fix the code, simplify it. &amp;nbsp;You could use a %do loop that reads:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do y=11 to 16;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using &amp;amp;Y, it should be easy to get the numerical pieces that you need. &amp;nbsp;For example, when &amp;amp;Y is 11 ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get 2011: &amp;nbsp;%eval(&amp;amp;y + 2000)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get 1112: &amp;nbsp;&amp;amp;y%eval(&amp;amp;y+1)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take out the complications.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 01:58:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371509#M88758</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-06-29T01:58:31Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop variable is not sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371510#M88759</link>
      <description>&lt;P&gt;Thanks. Here is the code&lt;/P&gt;
&lt;P&gt;%let last_YYnextYY=1617;&lt;BR /&gt;%macro get;&lt;BR /&gt;proc sql;&lt;BR /&gt; create table want as&lt;BR /&gt; %do YYnextYY = 1314 %to &amp;amp;last_YYnextYY %by 101;&lt;BR /&gt; select id, date from have&amp;amp;yynextYY.Q4&lt;BR /&gt; %if &amp;amp;YYnextYY=&amp;amp;last_YYnextYY %then union;&lt;BR /&gt; %end;&lt;BR /&gt;run;&lt;BR /&gt;%mend get;&lt;BR /&gt;%get;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And it got an errorr message. It looks like the union one is not placed right?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Error message: create table want as select id, date from have1314Q4 select id, date from have1415Q4 select id, date from have1516Q4 &amp;nbsp;select id, date from have1617Q4 union run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any tips? Sorry it's beyond my skills. Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 02:20:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371510#M88759</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2017-06-29T02:20:16Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop variable is not sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371511#M88760</link>
      <description>&lt;P&gt;Thanks for the advice. I knew it was clumsy. I''ll take note and modify.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 02:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371511#M88760</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2017-06-29T02:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop variable is not sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371513#M88762</link>
      <description>&lt;P&gt;First simplify the code you need to do the job. Using SQL is just going to make the problem harder and also you could run into limits that you will not have when using regular SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set HaveData1314Q4 
      HaveData1415Q4
      HaveData1516Q4
      HaveData1617Q4
  ;
  keep id date;
run;
proc sort nodupkey; 
  by id date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now to generate the list you could use macro code, but you could also just use normal code and store the generated list into a macro varaible.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length str $300;
  do yr=13 to 16 ;
     str=catx(' ',str,cats('HaveData',put(yr,z2.),put(yr+1,z2.),'Q4'));
  end;
  call symputx('dslist',str);
run;
data want ;
  set &amp;amp;dslist ;
  keep id date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Jun 2017 03:10:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371513#M88762</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-06-29T03:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop variable is not sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371536#M88769</link>
      <description>&lt;P&gt;You can take &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s code and use call execute to streamline it even more:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call execute('data want; set ');
  do yr=13 to 16 ;
    call execute(cats('HaveData',put(yr,z2.),put(yr+1,z2.),'Q4') !! ' ');
  end;
  call execute('; keep id date; run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Jun 2017 05:59:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371536#M88769</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-29T05:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop variable is not sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371669#M88798</link>
      <description>&lt;P&gt;Oops.&amp;nbsp; I didn't transcribe my program to the forum properly.&amp;nbsp; Instead of&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %if &amp;amp;YYnextYY=&amp;amp;last_YYnextYY %then union;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it should have been&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %if &amp;amp;YYnextYY&amp;nbsp; NE &amp;amp;last_YYnextYY %then union;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will edit the original note.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 13:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-variable-is-not-sequential/m-p/371669#M88798</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-06-29T13:00:51Z</dc:date>
    </item>
  </channel>
</rss>

