<?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: need help with do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711270#M219080</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I'm confused by your data and what you say you want:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1610567675927.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53477iEBF1855BD114D002/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1610567675927.png" alt="Cynthia_sas_0-1610567675927.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you show only 1 row per ID in your desired output but show 2 rows for J2256? It is not clear to me whether you expect to see 13 rows in the final output, 6 rows in the final output or 7 rows in the final output. Can you clarify?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jan 2021 20:17:48 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2021-01-13T20:17:48Z</dc:date>
    <item>
      <title>need help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711261#M219077</link>
      <description>&lt;P&gt;I've tried a variety of things. I can get the output to one row per r_id which is what I want but the result is not correct.&lt;/P&gt;
&lt;P&gt;If there is no strt_dt for a r_id then we want the earliest creat_dt.&lt;/P&gt;
&lt;P&gt;if there is a strt_dt for a r_id we want the earliest strt_dt.&lt;/P&gt;
&lt;P&gt;This is what the result should look like&lt;/P&gt;
&lt;P&gt;J1178 03/05/2014&lt;BR /&gt;J2256 03/22/2014&lt;BR /&gt;J2256 03/26/2014 &lt;BR /&gt;J3363 03/19/2014&lt;BR /&gt;J4444 03/24/2014&lt;BR /&gt;J5522 03/24/2014 &lt;BR /&gt;J8434 03/24/2014&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input @1 r_id $5. @7 creat_dttm mmddyy10. @18  strt_dt mmddyy10. ;
format creat_dttm strt_dt mmddyy10.;
cards;
J3363 03/17/2014 
J2256 03/26/2014 
J3363 03/18/2014 03/19/2014
J8434 03/19/2014 
J2256 03/24/2014 04/29/2014
J8434 03/20/2014 03/24/2014
J1178 03/05/2014 
J8434 03/11/2014 
J4444 03/21/2014 03/24/2014
J5522 03/24/2014 
J2256 03/26/2014 03/22/2014  
J3363 03/10/2014 03/21/2014
J1178 03/24/2014 
;
run;

proc sort data=have;
	by r_id strt_dt creat_dttm;
run;



data new;
	set have;
	by r_id strt_dt creat_dttm;
	format newdate maybe_dt mmddyy10.;
	if first.r_id then do;
		retain newdate .;
		retain maybe_dt .;
	end;
 	if first.r_id and strt_dt =. then do;
		maybe_dt=creat_dttm;
		put 'new' newdate;
	end;
	else if first.r_id and strt_dt !.  then newdate=strt_dt;
	else if newdate =. then newdate=strt_dt;
	if newdate =. then newdate=strt_dt;
	if last.r_id and newdate =. then do;
		newdate=maybe_dt;
		output;
	end;
/*	else if newdate !. then do;*/
/*		newdate=maybe_dt;*/
/*		output;*/
/*	end;*/
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any help is greatly appreciated. Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 19:35:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711261#M219077</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-01-13T19:35:27Z</dc:date>
    </item>
    <item>
      <title>Re: need help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711264#M219078</link>
      <description>&lt;P&gt;Try next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input @1 r_id $5. @7 creat_dttm mmddyy10. @18  strt_dt mmddyy10. ;
format creat_dttm strt_dt mmddyy10.;
cards;
J3363 03/17/2014 
J2256 03/26/2014 
J3363 03/18/2014 03/19/2014
J8434 03/19/2014 
J2256 03/24/2014 04/29/2014
J8434 03/20/2014 03/24/2014
J1178 03/05/2014 
J8434 03/11/2014 
J4444 03/21/2014 03/24/2014
J5522 03/24/2014 
J2256 03/26/2014 03/22/2014  
J3363 03/10/2014 03/21/2014
J1178 03/24/2014 
;
run;

proc sort data=have;
	by r_id creat_dttm strt_dt;
run;

data new;
 set have;
  by r_id;
    retain newdate flag;
	format newdate  mmddyy10.;
	if first.r_id then do;
	   newdate = creat_dttm;
	   flag = 0;
	end;
	if not missing(strt_dt) and flag=0
	then do; newdate = strt_dt; flag=1; end;
	if last.r_id then output;
	keep r_id newdate;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2021 19:54:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711264#M219078</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-13T19:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: need help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711267#M219079</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49285"&gt;@DanD999&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;If there is no strt_dt for a r_id then we want the earliest creat_dt.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;if there is a strt_dt for a r_id we want the earliest strt_dt.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If should be pretty straight forward to translate that into SQL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input  r_id :$5. creat_dttm :mmddyy. strt_dt :mmddyy. ;
  format creat_dttm strt_dt mmddyy10.;
cards;
J3363 03/17/2014 .
J2256 03/26/2014 .
J3363 03/18/2014 03/19/2014
J8434 03/19/2014 .
J2256 03/24/2014 04/29/2014
J8434 03/20/2014 03/24/2014
J1178 03/05/2014 .
J8434 03/11/2014 .
J4444 03/21/2014 03/24/2014
J5522 03/24/2014 .
J2256 03/26/2014 03/22/2014  
J3363 03/10/2014 03/21/2014
J1178 03/24/2014 .
;

proc sql noprint;
create table want as 
  select r_id
       , coalesce(min(strt_dt),min(creat_dttm)) as newdate format=mmddyy10.
  from have
  group by r_id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    r_id        newdate

 1     J1178    03/05/2014
 2     J2256    03/22/2014
 3     J3363    03/19/2014
 4     J4444    03/24/2014
 5     J5522    03/24/2014
 6     J8434    03/24/2014
&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2021 20:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711267#M219079</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-13T20:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: need help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711270#M219080</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I'm confused by your data and what you say you want:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1610567675927.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53477iEBF1855BD114D002/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1610567675927.png" alt="Cynthia_sas_0-1610567675927.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you show only 1 row per ID in your desired output but show 2 rows for J2256? It is not clear to me whether you expect to see 13 rows in the final output, 6 rows in the final output or 7 rows in the final output. Can you clarify?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 20:17:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711270#M219080</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2021-01-13T20:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: need help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711271#M219081</link>
      <description>&lt;P&gt;Excellent. That's it. Thank you so much for the help and fast response.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 20:31:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711271#M219081</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-01-13T20:31:05Z</dc:date>
    </item>
    <item>
      <title>Re: need help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711297#M219096</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49285"&gt;@DanD999&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;If there is no strt_dt for a r_id then we want the earliest creat_dt.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;if there is a strt_dt for a r_id we want the earliest strt_dt.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If should be pretty straight forward to translate that into SQL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input  r_id :$5. creat_dttm :mmddyy. strt_dt :mmddyy. ;
  format creat_dttm strt_dt mmddyy10.;
cards;
J3363 03/17/2014 .
J2256 03/26/2014 .
J3363 03/18/2014 03/19/2014
J8434 03/19/2014 .
J2256 03/24/2014 04/29/2014
J8434 03/20/2014 03/24/2014
J1178 03/05/2014 .
J8434 03/11/2014 .
J4444 03/21/2014 03/24/2014
J5522 03/24/2014 .
J2256 03/26/2014 03/22/2014  
J3363 03/10/2014 03/21/2014
J1178 03/24/2014 .
;

proc sql noprint;
create table want as 
  select r_id
       , coalesce(min(strt_dt),min(creat_dttm)) as newdate format=mmddyy10.
  from have
  group by r_id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    r_id        newdate

 1     J1178    03/05/2014
 2     J2256    03/22/2014
 3     J3363    03/19/2014
 4     J4444    03/24/2014
 5     J5522    03/24/2014
 6     J8434    03/24/2014
&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That does also work. Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 21:38:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711297#M219096</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-01-13T21:38:47Z</dc:date>
    </item>
    <item>
      <title>Re: need help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711299#M219098</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13549"&gt;@Cynthia_sas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I'm confused by your data and what you say you want:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1610567675927.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53477iEBF1855BD114D002/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1610567675927.png" alt="Cynthia_sas_0-1610567675927.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you show only 1 row per ID in your desired output but show 2 rows for J2256? It is not clear to me whether you expect to see 13 rows in the final output, 6 rows in the final output or 7 rows in the final output. Can you clarify?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I did say in the initial post "&lt;SPAN&gt;I can get the output to one row per r_id which is what I want"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks for the reply.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 21:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-do-loop/m-p/711299#M219098</guid>
      <dc:creator>DanD999</dc:creator>
      <dc:date>2021-01-13T21:40:27Z</dc:date>
    </item>
  </channel>
</rss>

