<?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 Help with do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/499366#M132872</link>
    <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my data I need to calculate number of days between visit dates.&amp;nbsp; The "date" variable in the below data set contains numeric dates. With my below do loop code i am able to calculate difference two adjacent dates. But I need to calculate difference between all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;eg: In first row there are 5 date values. My code calculating difference between&amp;nbsp; first and second, third and second and so on. But I also need to calculate third and first, fourth and first, fourth and second and so on. How to code this process to work in the below code&lt;/P&gt;
&lt;P&gt;Please help. Thank you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input a $1-26 date $27-80;
datalines;
PR PR PR PR CR CR          21109 21171 21227 21290 21350 21433
PR CR CR CR CR CR CR CR CR 20803 20853 20915 20978 21041 21105 21188 21279 21357
CR CR CR CR CR             21119 21187 21243 21312 21370
CR CR PR                   21272 21329 21384
;

data two;
set one;
cw = countw(a);
array diff{9} ;
do i= 1 to cw;
 diff(i)=scan(date,i+1)-scan(date,i);
end;
run;
    &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 27 Sep 2018 04:32:59 GMT</pubDate>
    <dc:creator>knveraraju91</dc:creator>
    <dc:date>2018-09-27T04:32:59Z</dc:date>
    <item>
      <title>Help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/499366#M132872</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my data I need to calculate number of days between visit dates.&amp;nbsp; The "date" variable in the below data set contains numeric dates. With my below do loop code i am able to calculate difference two adjacent dates. But I need to calculate difference between all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;eg: In first row there are 5 date values. My code calculating difference between&amp;nbsp; first and second, third and second and so on. But I also need to calculate third and first, fourth and first, fourth and second and so on. How to code this process to work in the below code&lt;/P&gt;
&lt;P&gt;Please help. Thank you&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input a $1-26 date $27-80;
datalines;
PR PR PR PR CR CR          21109 21171 21227 21290 21350 21433
PR CR CR CR CR CR CR CR CR 20803 20853 20915 20978 21041 21105 21188 21279 21357
CR CR CR CR CR             21119 21187 21243 21312 21370
CR CR PR                   21272 21329 21384
;

data two;
set one;
cw = countw(a);
array diff{9} ;
do i= 1 to cw;
 diff(i)=scan(date,i+1)-scan(date,i);
end;
run;
    &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Sep 2018 04:32:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/499366#M132872</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2018-09-27T04:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: Help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/499370#M132876</link>
      <description>&lt;P&gt;Put your data in long form first. Then perform a cartesian self join with SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input a $1-26 date $27-80;
datalines;
PR PR PR PR CR CR          21109 21171 21227 21290 21350 21433
PR CR CR CR CR CR CR CR CR 20803 20853 20915 20978 21041 21105 21188 21279 21357
CR CR CR CR CR             21119 21187 21243 21312 21370
CR CR PR                   21272 21329 21384
;

data two;
set one;
line = _n_;
length r $8;
do i = 1 to countw(a);
    r = scan(a, i);
    dt = input(scan(date, i), best.);
    output;
    end;
format dt date9.;
keep line r dt i;
run;

proc sql;
select 
    a.line, 
    a.r as a_1, 
    a.dt as dt_1, 
    b.r as a_2, 
    b.dt as dt_2,
    intck('day', a.dt, b.dt)
from two as a inner join two as b on a.line=b.line and a.i &amp;lt; b.i
order by line, a.i, b.i;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Sep 2018 05:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/499370#M132876</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-27T05:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: Help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/499389#M132883</link>
      <description>&lt;P&gt;With up to 9 dates, that would require 36 differences.&amp;nbsp; What do you plan to name those variables?&lt;/P&gt;</description>
      <pubDate>Thu, 27 Sep 2018 07:42:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/499389#M132883</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-09-27T07:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: Help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/502459#M134131</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for the code.&amp;nbsp; The worked for me. But i need to add two more logic in my code to make efficient when new data added.&lt;/P&gt;
&lt;P&gt;I need help in proc sql code below for id=7 and 8. These subjects have two or more 'NE' between two 'CR' and two 'PR'.&amp;nbsp; I am attaching a document showing how the output look like in after proc sql step. For highlighted in yellow OBS, I need to apply "intck('day', a.rdate,a.dt)"&amp;nbsp; if two or more "NE" between two 'CR' (for id=7).&amp;nbsp; I need to apply "intck('day', a.dt, b.dt)" if two or 'NE' between two PR (for id=8). Please help. Thank you very much.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output needed:&lt;/P&gt;
&lt;P&gt;for OBS=70 the 'nod' variable value&amp;nbsp; should be 665.&lt;/P&gt;
&lt;P&gt;for obs =73 the 'nod' variable value should be as shown in output(193)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id rd $10. a $14-39 date $41-93;
datalines;
1 2016-01-01 PR CR CR CR CR CR CR CR CR 20803 20853 20915 20978 21041 21105 21188 21279 21357
2 2016-01-01 PR PR PR PR CR CR          21109 21171 21227 21290 21350 21433
3 2016-01-01 CR CR CR CR CR             21119 21187 21243 21312 21370
4 2016-01-01 CR CR PR                   21272 21329 21384
5 2016-01-01 CR NE                      21272 21329
6 2016-01-01 CR NE CR                   21272 21329 21384
7 2016-01-01 CR NE NE CR                21119 21187 21243 21312
8 2016-01-01 PR NE NE PR                21119 21187 21243 21312
;

data two;
set one;
line = _n_;
length r $8;
do i = 1 to countw(a);
    r = scan(a, i);
    dt = input(scan(date, i), best.);
	rdate=input(rd,yymmdd10.);
    output;
    end;
	
format dt rdate date9.;
keep id line r dt i rdate;
run;

proc sql;
create table four as
select 
    a.id, a.rdate,
    a.r as a_1, 
    a.dt as dt_1, 
    b.r as a_2, 
    b.dt as dt_2,
	case 
     when a_1 in ('CR' 'PR') and a_2 in ('CR' 'PR')then 
    intck('day', a.dt, b.dt) 
    when a_1 in ('CR' 'PR') and a_2 not in ('CR' 'PR') then intck('day', a.rdate,a.dt) end as nod
from two as a inner join two as b on a.id=b.id and a.i &amp;lt; b.i
where calculated nod ^= .
order by id, a.i, b.i;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Oct 2018 17:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/502459#M134131</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2018-10-08T17:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: Help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/502479#M134141</link>
      <description>&lt;P&gt;I don't see how to express te condition " if two or more "NE" between two 'CR'&amp;nbsp;" in SQL. It is not clear which comparisons will be affected; those between NEs, or those between NEs and CRs ?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 18:52:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/502479#M134141</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-10-08T18:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/502827#M134284</link>
      <description>&lt;P&gt;Thank you very much for reply. I will try to explain what i need in my pgm. If not clear sorry. I will try if i can write the logic in data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. In my pgm, I calculate the difference between any two dates if both responses are in 'CR' 'PR'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;eg. if the first response is either 'CR' or 'PR'&amp;nbsp; and second(NEXT) response also has to be 'CR' and 'PR'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. &lt;SPAN&gt;if the first response is either 'CR' or 'PR'&amp;nbsp; and second(NEXT) response &lt;FONT color="#FF0000"&gt;not&lt;/FONT&gt; in 'CR' or 'PR'. then i calculate difference between date of first response date and randamization date(rdate in my pgm).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The Proc sql pgm works fine for this.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;But for id=7, where responses are&amp;nbsp; CR NE NE CR ,&amp;nbsp; the sql code calculates&amp;nbsp;as below.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1.CR(first) -NE(first)&amp;nbsp; (date of CR-date of rdate)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2.CR-NE(second one)&amp;nbsp; &amp;nbsp;(date of CR-date of rdate)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3. CR(first)-CR(second) (date of CR-date of CR)&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I need help in&amp;nbsp; step 3 where it should be calculating&amp;nbsp;&amp;nbsp; &amp;nbsp;(date of CR(first) -date of rdate) as there are 2 or more NE&amp;nbsp; between two CR&lt;/SPAN&gt;&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt; &amp;nbsp;&lt;/SPAN&gt;&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>Tue, 09 Oct 2018 17:53:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-do-loop/m-p/502827#M134284</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2018-10-09T17:53:05Z</dc:date>
    </item>
  </channel>
</rss>

