<?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: Getting values of a variable from previous set of observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31134#M5981</link>
    <description>Have a look at the LAG function - lag1(), lag2()&lt;BR /&gt;
&lt;A href="http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000212547.htm" target="_blank"&gt;http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000212547.htm&lt;/A&gt;</description>
    <pubDate>Wed, 13 May 2009 07:09:35 GMT</pubDate>
    <dc:creator>GertNissen</dc:creator>
    <dc:date>2009-05-13T07:09:35Z</dc:date>
    <item>
      <title>Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31133#M5980</link>
      <description>patid week day dose&lt;BR /&gt;
1	1   1   10&lt;BR /&gt;
1	1   3   11&lt;BR /&gt;
1	2   1   12&lt;BR /&gt;
1	2   3   13&lt;BR /&gt;
1	3   1   13&lt;BR /&gt;
1	4   1   14&lt;BR /&gt;
1	5   1   15&lt;BR /&gt;
1	5   3   16&lt;BR /&gt;
1	6   1   17&lt;BR /&gt;
1	6   3   18&lt;BR /&gt;
At any iteration of datastep, at day1 I want to get dose values of previous two weeks.&lt;BR /&gt;
For example at week5 day1 I should be able to access values of dose for weeks 3 and 4.  for week6 I should be able to get dose values of week5 and week4.&lt;BR /&gt;
how can I accomplish this. help me.</description>
      <pubDate>Wed, 13 May 2009 05:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31133#M5980</guid>
      <dc:creator>A_B_C</dc:creator>
      <dc:date>2009-05-13T05:34:33Z</dc:date>
    </item>
    <item>
      <title>Re: Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31134#M5981</link>
      <description>Have a look at the LAG function - lag1(), lag2()&lt;BR /&gt;
&lt;A href="http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000212547.htm" target="_blank"&gt;http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000212547.htm&lt;/A&gt;</description>
      <pubDate>Wed, 13 May 2009 07:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31134#M5981</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2009-05-13T07:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31135#M5982</link>
      <description>A_B_C,&lt;BR /&gt;
&lt;BR /&gt;
You could read the data from the prior 2 weeks using multiple set statements.&lt;BR /&gt;
VIZ:&lt;BR /&gt;
data a;                                                   &lt;BR /&gt;
input patid week day dose;                                &lt;BR /&gt;
Infile cards;                                             &lt;BR /&gt;
cards;                                                    &lt;BR /&gt;
1 1 1 10                                                  &lt;BR /&gt;
1 1 3 11                                                  &lt;BR /&gt;
1 2 1 12                                                  &lt;BR /&gt;
1 2 3 13                                                  &lt;BR /&gt;
1 3 1 13                                                  &lt;BR /&gt;
1 4 1 14                                                  &lt;BR /&gt;
1 5 1 15                                                  &lt;BR /&gt;
1 5 3 16                                                  &lt;BR /&gt;
1 6 1 17                                                  &lt;BR /&gt;
1 6 3 18                                                  &lt;BR /&gt;
;                                                         &lt;BR /&gt;
run;                                                      &lt;BR /&gt;
                                                          &lt;BR /&gt;
data b;                                                   &lt;BR /&gt;
set a;                                                    &lt;BR /&gt;
if _n_ &amp;gt; 1 then set a(keep = dose rename=(dose=dosemin1));&lt;BR /&gt;
if _n_ &amp;gt; 2 then set a(keep = dose rename=(dose=dosemin2));&lt;BR /&gt;
run;                                                      &lt;BR /&gt;
                                                          &lt;BR /&gt;
proc print data=b;                                        &lt;BR /&gt;
run;                                                      &lt;BR /&gt;
&lt;BR /&gt;
This produces the output shown below. Once enough data is present you simply look along the observation to look-up the last and last-1 positions.&lt;BR /&gt;
&lt;BR /&gt;
Obs    patid    week    day    dose    dosemin1    dosemin2&lt;BR /&gt;
&lt;BR /&gt;
  1      1        1      1      10         .           .&lt;BR /&gt;
  2      1        1      3      11        10           .&lt;BR /&gt;
  3      1        2      1      12        11          10&lt;BR /&gt;
  4      1        2      3      13        12          11&lt;BR /&gt;
  5      1        3      1      13        13          12&lt;BR /&gt;
  6      1        4      1      14        13          13&lt;BR /&gt;
  7      1        5      1      15        14          13&lt;BR /&gt;
  8      1        5      3      16        15          14&lt;BR /&gt;
  9      1        6      1      17        16          15&lt;BR /&gt;
 10      1        6      3      18        17          16&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
&lt;BR /&gt;
BPD</description>
      <pubDate>Wed, 13 May 2009 07:46:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31135#M5982</guid>
      <dc:creator>BPD</dc:creator>
      <dc:date>2009-05-13T07:46:08Z</dc:date>
    </item>
    <item>
      <title>Re: Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31136#M5983</link>
      <description>Hi BPD,&lt;BR /&gt;
This is fine. what if I  need to get previous 4 weeks data. If I use the same logic, it creates more variables. I just want to know is there any other way that can be used  to solve this problem? or is it the only way to do it. I dont think use of lag function is a right choice as there will be missing dose at some weeks.</description>
      <pubDate>Wed, 13 May 2009 10:33:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31136#M5983</guid>
      <dc:creator>A_B_C</dc:creator>
      <dc:date>2009-05-13T10:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31137#M5984</link>
      <description>It would be useful to know exactly what you are trying to do - saying you need to "access" is unclear.  That said, this code does what I think you are asking for, and it will work with any number of weeks.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
* Create week totals for each patid;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  create table test2 as select distinct patid,week,sum(dose) as WeekTtl&lt;BR /&gt;
    from test group by patid,week order by patid,week;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
* Flip long to wide to get values from every week onto one row;&lt;BR /&gt;
proc transpose data=test2 out=test2t(drop=_NAME_) prefix=Week;&lt;BR /&gt;
  by patid;&lt;BR /&gt;
  id week;&lt;BR /&gt;
  var WeekTtl;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* Merge transposed data back with original data by patid;&lt;BR /&gt;
data test3;&lt;BR /&gt;
  merge test test2t;&lt;BR /&gt;
  by patid;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
===============&lt;BR /&gt;
&lt;BR /&gt;
if you do not want to use sql, this duplicates that step.&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=test out=test4;&lt;BR /&gt;
  by patid week day;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data test4(keep=patid week WeekTtl);&lt;BR /&gt;
  retain WeekTtl;&lt;BR /&gt;
  set test4;&lt;BR /&gt;
  by patid week day;&lt;BR /&gt;
  if first.week then WeekTtl=dose;&lt;BR /&gt;
  else WeekTtl=WeekTtl+dose;&lt;BR /&gt;
  if last.week then output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
ED</description>
      <pubDate>Wed, 13 May 2009 14:11:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31137#M5984</guid>
      <dc:creator>edilts</dc:creator>
      <dc:date>2009-05-13T14:11:31Z</dc:date>
    </item>
    <item>
      <title>Re: Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31138#M5985</link>
      <description>by-group on patient, collecting dosage in an array by week/day with as many entries as your data history would need for a patient. &lt;BR /&gt;
At last row for patient, analyse over week/day: For a patient, on any day with history, examine the array up to that day, to determine whatever you need. &lt;BR /&gt;
If you need one year of history, define the array for  52 weeks and 7 days like[pre]   array dose_hist( 52,7 ) ; [/pre] accessed like [pre]   dose_hist( week,day ) = dosage ; [/pre]&lt;BR /&gt;
 &lt;BR /&gt;
PeterC</description>
      <pubDate>Wed, 13 May 2009 14:21:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31138#M5985</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-05-13T14:21:19Z</dc:date>
    </item>
    <item>
      <title>Re: Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31139#M5986</link>
      <description>A_B_C,&lt;BR /&gt;
&lt;BR /&gt;
I hadn't realised from your original post that you wanted that flexibility. &lt;BR /&gt;
&lt;BR /&gt;
It's a simple step to put the code you require into a macro. For example:&lt;BR /&gt;
&lt;BR /&gt;
data a; &lt;BR /&gt;
input patid week day dose; &lt;BR /&gt;
Infile cards; &lt;BR /&gt;
cards; &lt;BR /&gt;
1 1 1 10 &lt;BR /&gt;
1 1 3 11 &lt;BR /&gt;
1 2 1 12 &lt;BR /&gt;
1 2 3 13 &lt;BR /&gt;
1 3 1 13 &lt;BR /&gt;
1 4 1 14 &lt;BR /&gt;
1 5 1 15 &lt;BR /&gt;
1 5 3 16 &lt;BR /&gt;
1 6 1 17 &lt;BR /&gt;
1 6 3 18 &lt;BR /&gt;
; &lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
/* Now define the macro - note that timst and timend allow you to name the&lt;BR /&gt;
   start and stop months that you're interested in. */&lt;BR /&gt;
&lt;BR /&gt;
%MACRO reread(track=,timst=,timend=,in=a,out=b);&lt;BR /&gt;
DATA &amp;amp;out; &lt;BR /&gt;
SET &amp;amp;in; &lt;BR /&gt;
%DO i = &amp;amp;timst %TO %EVAL(&amp;amp;timend);&lt;BR /&gt;
 IF _n_ &amp;gt; &amp;amp;i THEN SET &amp;amp;in(KEEP = &amp;amp;track RENAME=(&amp;amp;track=&amp;amp;track.min&amp;amp;i));&lt;BR /&gt;
%END;&lt;BR /&gt;
RUN; &lt;BR /&gt;
%MEND reread;&lt;BR /&gt;
&lt;BR /&gt;
 /* now - to track the variable dose for 2 to 3 months ago */&lt;BR /&gt;
&lt;BR /&gt;
%reread(track=dose,timst=2,timend=3,in=a,out=b);&lt;BR /&gt;
&lt;BR /&gt;
proc print data=b; &lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
/* the above print gives the following output.               */&lt;BR /&gt;
&lt;BR /&gt;
Obs    patid    week    day    dose    dosemin2    dosemin3&lt;BR /&gt;
&lt;BR /&gt;
  1      1        1      1      10         .           .&lt;BR /&gt;
  2      1        1      3      11         .           .&lt;BR /&gt;
  3      1        2      1      12        10           .&lt;BR /&gt;
  4      1        2      3      13        11          10&lt;BR /&gt;
  5      1        3      1      13        12          11&lt;BR /&gt;
  6      1        4      1      14        13          12&lt;BR /&gt;
  7      1        5      1      15        13          13&lt;BR /&gt;
  8      1        5      3      16        14          13&lt;BR /&gt;
  9      1        6      1      17        15          14&lt;BR /&gt;
 10      1        6      3      18        16          15&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Perhaps this is addresses your needs more accurately?&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
&lt;BR /&gt;
BPD</description>
      <pubDate>Thu, 14 May 2009 08:02:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31139#M5986</guid>
      <dc:creator>BPD</dc:creator>
      <dc:date>2009-05-14T08:02:38Z</dc:date>
    </item>
    <item>
      <title>Re: Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31140#M5987</link>
      <description>this looks fine. But if you look at week5 day1, it is not giving the previous week week4 value rather its giving values of week3 only. Another thing is that, again it is difficult to know which dose column represents which particular week.&lt;BR /&gt;
For example at wee5 day1 it is difficult to know what are the dose columns that I need to use to get the values, might be simple for 2 weeks but if I want to use 4 weeks, it seems more tricky</description>
      <pubDate>Thu, 14 May 2009 10:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31140#M5987</guid>
      <dc:creator>A_B_C</dc:creator>
      <dc:date>2009-05-14T10:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31141#M5988</link>
      <description>that's why I suggest a patient array by week,day.&lt;BR /&gt;
You are able to review multiple weeks together</description>
      <pubDate>Thu, 14 May 2009 10:39:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31141#M5988</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-05-14T10:39:07Z</dc:date>
    </item>
    <item>
      <title>Re: Getting values of a variable from previous set of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31142#M5989</link>
      <description>I tried out as you suggested, but still not able to get values previous week exactly.&lt;BR /&gt;
here goes the code and output.&lt;BR /&gt;
data a; &lt;BR /&gt;
input patid week day dose; &lt;BR /&gt;
Infile cards; &lt;BR /&gt;
cards; &lt;BR /&gt;
1 1 1 10 &lt;BR /&gt;
1 1 3 11 &lt;BR /&gt;
1 2 1 12 &lt;BR /&gt;
1 2 3 13 &lt;BR /&gt;
1 3 1 13 &lt;BR /&gt;
1 4 1 14 &lt;BR /&gt;
1 5 1 15 &lt;BR /&gt;
1 5 3 16 &lt;BR /&gt;
1 6 1 17 &lt;BR /&gt;
1 6 3 18 &lt;BR /&gt;
; &lt;BR /&gt;
run; &lt;BR /&gt;
data b;&lt;BR /&gt;
  set a;&lt;BR /&gt;
   by patid;&lt;BR /&gt;
   array a(6,3);&lt;BR /&gt;
    a(week,day)=dose;&lt;BR /&gt;
 run;&lt;BR /&gt;
 proc print;run;&lt;BR /&gt;
&lt;BR /&gt;
data a; &lt;BR /&gt;
input patid week day dose; &lt;BR /&gt;
Infile cards; &lt;BR /&gt;
cards; &lt;BR /&gt;
1 1 1 10 &lt;BR /&gt;
1 1 3 11 &lt;BR /&gt;
1 2 1 12 &lt;BR /&gt;
1 2 3 13 &lt;BR /&gt;
1 3 1 13 &lt;BR /&gt;
1 4 1 14 &lt;BR /&gt;
1 5 1 15 &lt;BR /&gt;
1 5 3 16 &lt;BR /&gt;
1 6 1 17 &lt;BR /&gt;
1 6 3 18 &lt;BR /&gt;
; &lt;BR /&gt;
run; &lt;BR /&gt;
data b;&lt;BR /&gt;
  set a;&lt;BR /&gt;
   by patid;&lt;BR /&gt;
   array a(6,3);&lt;BR /&gt;
    a(week,day)=dose;&lt;BR /&gt;
 run;&lt;BR /&gt;
 proc print;run;&lt;BR /&gt;
data a; &lt;BR /&gt;
input patid week day dose; &lt;BR /&gt;
Infile cards; &lt;BR /&gt;
cards; &lt;BR /&gt;
1 1 1 10 &lt;BR /&gt;
1 1 3 11 &lt;BR /&gt;
1 2 1 12 &lt;BR /&gt;
1 2 3 13 &lt;BR /&gt;
1 3 1 13 &lt;BR /&gt;
1 4 1 14 &lt;BR /&gt;
1 5 1 15 &lt;BR /&gt;
1 5 3 16 &lt;BR /&gt;
1 6 1 17 &lt;BR /&gt;
1 6 3 18 &lt;BR /&gt;
; &lt;BR /&gt;
run; &lt;BR /&gt;
data b;&lt;BR /&gt;
  set a;&lt;BR /&gt;
   by patid;&lt;BR /&gt;
   array a(6,3);&lt;BR /&gt;
    a(week,day)=dose;&lt;BR /&gt;
 run;&lt;BR /&gt;
 proc print;run;&lt;BR /&gt;
Obs&lt;BR /&gt;
patid	week	day	dose	a1	a2	a3	a4	a5	a6	a7	a8	a9	a10	a11	a12	a13	a14	a15	a16	a17	a18&lt;BR /&gt;
1	1	1	1	10	10	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.&lt;BR /&gt;
2	1	1	3	11	.	.	11	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.&lt;BR /&gt;
3	1	2	1	12	.	.	.	12	.	.	.	.	.	.	.	.	.	.	.	.	.	.&lt;BR /&gt;
4	1	2	3	13	.	.	.	.	.	13	.	.	.	.	.	.	.	.	.	.	.	.&lt;BR /&gt;
5	1	3	1	13	.	.	.	.	.	.	13	.	.	.	.	.	.	.	.	.	.	.&lt;BR /&gt;
6	1	4	1	14	.	.	.	.	.	.	.	.	.	14	.	.	.	.	.	.	.	.&lt;BR /&gt;
7	1	5	1	15	.	.	.	.	.	.	.	.	.	.	.	.	15	.	.	.	.	.&lt;BR /&gt;
8	1	5	3	16	.	.	.	.	.	.	.	.	.	.	.	.	.	.	16	.	.	.&lt;BR /&gt;
9	1	6	1	17	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.	17	.	.&lt;BR /&gt;
10	1	6	3	18	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.	18&lt;BR /&gt;
&lt;BR /&gt;
As this output contains missing columns of array, I think it is difficult to find which column value represents which week/day value. For example at week5 day1, still I am not able use week 4 value. please let me know if I need to any other changes to above code to resolve my issue</description>
      <pubDate>Thu, 14 May 2009 11:15:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-values-of-a-variable-from-previous-set-of-observations/m-p/31142#M5989</guid>
      <dc:creator>A_B_C</dc:creator>
      <dc:date>2009-05-14T11:15:28Z</dc:date>
    </item>
  </channel>
</rss>

