<?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 while condition on other dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236763#M268234</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/6466"&gt;@ynchoir&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The thing is I need to go backward from begindate by every 6 months up to 2002. &amp;nbsp;So I do not know how long I need to loop, so I cannot let n from 1 to 183. &amp;nbsp;If the begin date is 2003, I want to loop only from 1 to 2, not 183.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Yosh&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Can you post examples of what you mean by that? Are you saying you want one record for every 6 months? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you mean counting down and stopping if the generated date is before 01JAN2002? &amp;nbsp;So if the date is in 2015 then you would&amp;nbsp;have about 26 records?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data calendar ;
   set data0;
   format date date9.;
   do nmonth=-6 by -6 until (date &amp;lt; '01JAN2002'd);
      date = intnx('month',begindate,nmonth);
      if date &amp;gt;= '01JAN2002'd then output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 27 Nov 2015 17:04:32 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2015-11-27T17:04:32Z</dc:date>
    <item>
      <title>Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236748#M268226</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a question about how to use do while loop, when the condition depends on the value from other datasets.&lt;/P&gt;
&lt;P&gt;Specifically, I want to create a semiannual calender dating back from various starting dates to 2002.&lt;/P&gt;
&lt;P&gt;Say, data0 contains a list of 100 dates ('begindate'), tagged by a variable 'id'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I wrote was:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro makecalender;&lt;/P&gt;
&lt;P&gt;%Do i = 1 %to 100;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data temp;&lt;/P&gt;
&lt;P&gt;set data0;&lt;/P&gt;
&lt;P&gt;if id = &amp;amp;i;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data calender&amp;amp;i;&lt;/P&gt;
&lt;P&gt;n = 0;&lt;/P&gt;
&lt;P&gt;set temp;&lt;/P&gt;
&lt;P&gt;do while (begindate - n*183 &amp;gt; '1JAN2002'd);&lt;/P&gt;
&lt;P&gt;date = begindate - n*183;&lt;/P&gt;
&lt;P&gt;output;&lt;/P&gt;
&lt;P&gt;n+1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;i=1 %then %do;&lt;/P&gt;
&lt;P&gt;data calender;&lt;/P&gt;
&lt;P&gt;set calender&amp;amp;i;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;i&amp;gt;1 %then %do;&lt;/P&gt;
&lt;P&gt;data calender;&lt;/P&gt;
&lt;P&gt;set calender calender&amp;amp;i;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%end&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which did not work. &amp;nbsp;The issue seems to lie on the line "do while (begindate - n*183 &amp;gt; '1JAN2002'd);", as when I replaced "begindate-n*183" with some specific inputs (like "27NOV2015"d) then it worked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could someone suggest how I might fix this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Yosh&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Nov 2015 16:19:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236748#M268226</guid>
      <dc:creator>ynchoir</dc:creator>
      <dc:date>2015-11-27T16:19:12Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236750#M268227</link>
      <description>&lt;P&gt;If you give an example of the data you have and what you are trying to achieve it will be very helpful.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Nov 2015 16:22:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236750#M268227</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2015-11-27T16:22:43Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236753#M268228</link>
      <description>Well...what does begindate look like, is it a SAS date, numeric type and formatted with a date format?&lt;BR /&gt;&lt;BR /&gt;Check the proc contents results for the dataset.</description>
      <pubDate>Fri, 27 Nov 2015 16:23:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236753#M268228</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-11-27T16:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236755#M268229</link>
      <description>&lt;P&gt;You can replace this section with a PROC APPEND that deals with the fact that the table may not exist on the first iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;i=1 %then %do;
data calender;
set calender&amp;amp;i;
run;
 
%end;
%if &amp;amp;i&amp;gt;1 %then %do;
data calender;
set calender calender&amp;amp;i;
run;
%end&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Becomes&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*You may or may not want the force option;
Proc append base=Calendar data=calendar&amp;amp;i FORCE;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, instead of date-n*183 you may want to look into the INTNX function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;INTNX('day', begindate, -n*183)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Nov 2015 16:35:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236755#M268229</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-11-27T16:35:42Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236756#M268230</link>
      <description>&lt;P&gt;So you want to take a dataset with 100 observations and blow it up into a dataset with 18,300 observations?&lt;/P&gt;
&lt;P&gt;Why would you need to use macor code for that?&lt;/P&gt;
&lt;P&gt;Let's make some sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data0;
   id+1;
   input begindate :date9. @@ ;
   format begindate date9.;
cards;
01JAN1983 06MAR2001
;;;;  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's use a DO loop to generate the file with 183 times as many records and one new variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data calendar;
   set data0;
   do date=begindate-183 to begindate;
      output;
   end;
   format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Nov 2015 16:43:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236756#M268230</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-11-27T16:43:53Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236758#M268231</link>
      <description>&lt;P&gt;Thanks! &amp;nbsp;&lt;/P&gt;
&lt;P&gt;begindate is numeric value, formatted in DATE9.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Nov 2015 16:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236758#M268231</guid>
      <dc:creator>ynchoir</dc:creator>
      <dc:date>2015-11-27T16:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236759#M268232</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The thing is I need to go backward from begindate by every 6 months up to 2002. &amp;nbsp;So I do not know how long I need to loop, so I cannot let n from 1 to 183. &amp;nbsp;If the begin date is 2003, I want to loop only from 1 to 2, not 183.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Yosh&lt;/P&gt;</description>
      <pubDate>Fri, 27 Nov 2015 16:55:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236759#M268232</guid>
      <dc:creator>ynchoir</dc:creator>
      <dc:date>2015-11-27T16:55:08Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236760#M268233</link>
      <description>Use the INTNX function and your loop end/start can be a variable so you should be able to do this without macro code. Post some sample data and expected output.</description>
      <pubDate>Fri, 27 Nov 2015 16:56:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236760#M268233</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-11-27T16:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236763#M268234</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/6466"&gt;@ynchoir&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The thing is I need to go backward from begindate by every 6 months up to 2002. &amp;nbsp;So I do not know how long I need to loop, so I cannot let n from 1 to 183. &amp;nbsp;If the begin date is 2003, I want to loop only from 1 to 2, not 183.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Yosh&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Can you post examples of what you mean by that? Are you saying you want one record for every 6 months? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you mean counting down and stopping if the generated date is before 01JAN2002? &amp;nbsp;So if the date is in 2015 then you would&amp;nbsp;have about 26 records?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data calendar ;
   set data0;
   format date date9.;
   do nmonth=-6 by -6 until (date &amp;lt; '01JAN2002'd);
      date = intnx('month',begindate,nmonth);
      if date &amp;gt;= '01JAN2002'd then output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Nov 2015 17:04:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236763#M268234</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-11-27T17:04:32Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236764#M268235</link>
      <description>&lt;P&gt;Here's an example. Use intck to calculate the number of intervals needed for you loop and then a loop. You may want to align your dates within the periods as well, either to the start or end.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Generate sample data*/
data have;
informat date date9.;
format date date9.;
input ID date;
cards;
1 01Jan2015
2 28Feb2015
3 03Jan2011
4 04Feb2005
5 13Apr2004
6 19Jul2007
;
run;

data want;
set have;
*Calculate the number of loops need for interval;
intervals=intck('month6', mdy(1,1, 2002), date);
*Keep start date for comparison/checking;
Date_Start=Date;

do i=1 to intervals;
	*increment date, and create dates aligned to start/end of period for convenience;
	date=intnx('month6', date, -1, 's');
	Date_Aligned_Beginning=intnx('month6', date, 0, 'b');
	Date_Aligned_End=intnx('month6', date, 0, 'e');
	output;
end;

format date_aligned: date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Nov 2015 17:16:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236764#M268235</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-11-27T17:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236771#M268236</link>
      <description>&lt;P&gt;It seems like you just want every 183 days, as many as needed. &amp;nbsp;That doesn't take macro language, and is probably as simple as:&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;set have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;do date = begindate to '01jan2002'd by -183;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp;output;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Really, that's the whole program ... no macro language, just a simple loop.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Nov 2015 19:43:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236771#M268236</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-11-27T19:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: Do while condition on other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236779#M268237</link>
      <description>&lt;P&gt;Thank you very much! &amp;nbsp;Yes, this code does what I wanted to do. &amp;nbsp;Much appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Yosh&lt;/P&gt;</description>
      <pubDate>Fri, 27 Nov 2015 20:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-while-condition-on-other-dataset/m-p/236779#M268237</guid>
      <dc:creator>ynchoir</dc:creator>
      <dc:date>2015-11-27T20:59:30Z</dc:date>
    </item>
  </channel>
</rss>

