<?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: Matching later values of a variable to values as of a given date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Matching-later-values-of-a-variable-to-values-as-of-a-given-date/m-p/244668#M45605</link>
    <description>&lt;P&gt;Hi there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming I have understood your problem correctly you will probably want something along these lines (I added some extra ID records to test it).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will need tidying up but this gives you what you asked for in the way of indicators and dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Chris&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt; infile datalines;&lt;BR /&gt; input&lt;BR /&gt; &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46433"&gt;@01﻿&lt;/a&gt; id $1.&lt;BR /&gt; @08 dt1 yymmn6.&lt;BR /&gt; @17 dt2 mmddyy10.&lt;BR /&gt; &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/29191"&gt;@33﻿&lt;/a&gt; dt3 mmddyy10.&lt;BR /&gt; @43 cat1 $3.&lt;BR /&gt; @54 cat2 $1.&lt;BR /&gt; ;&lt;BR /&gt;datalines;&lt;BR /&gt;1 201001 1/1/2009 . abc a&lt;BR /&gt;1 201001 1/25/2010 . def b&lt;BR /&gt;1 201002 1/1/2009 . abc a&lt;BR /&gt;1 201002 1/25/2010 2/3/2010 def b&lt;BR /&gt;1 201003 1/1/2009 . abc a&lt;BR /&gt;1 201003 3/12/2010 3/12/2010 ghi b&lt;BR /&gt;2 201001 1/1/2009 . abc a&lt;BR /&gt;2 201001 1/25/2010 . def b&lt;BR /&gt;2 201002 1/1/2009 . abc a&lt;BR /&gt;2 201002 1/25/2010 2/3/2010 def b&lt;BR /&gt;2 201003 1/1/2009 . abc a&lt;BR /&gt;3 201001 1/1/2009 . abc a&lt;BR /&gt;3 201001 1/25/2010 . def c&lt;BR /&gt;3 201002 1/1/2009 . abc a&lt;BR /&gt;3 201002 1/25/2010 2/3/2010 def b&lt;BR /&gt;3 201003 1/1/2009 . abc a&lt;BR /&gt;3 201003 3/12/2010 3/12/2010 ghi b&lt;BR /&gt;4 201001 1/1/2009 . abc a&lt;BR /&gt;4 201001 1/25/2010 . def b&lt;BR /&gt;4 201002 1/1/2009 . abc a&lt;BR /&gt;4 201002 1/25/2010 2/3/2010 def b&lt;BR /&gt;4 201003 1/1/2009 . abc a&lt;BR /&gt;4 201003 3/12/2010 3/12/2010 ghi b&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;* sort into each date column order within id (probably don't need to sort by the cat columns);&lt;/P&gt;
&lt;P&gt;proc sort data = have;&lt;BR /&gt; by id dt1 dt2 dt3 cat1 cat2;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;* set flags to meet requirements;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; by id dt1 dt2 dt3 cat1 cat2;&lt;/P&gt;
&lt;P&gt;* retain the indicators and earliest dates;&lt;/P&gt;
&lt;P&gt;retain &lt;BR /&gt; def_ind&lt;BR /&gt; dt2_first&lt;BR /&gt; cat2_val&lt;BR /&gt; dt3_first&lt;BR /&gt; ghi_ind&lt;BR /&gt; dt2_ghi&lt;BR /&gt; ;&lt;/P&gt;
&lt;P&gt;* initialise indicators and earliest dates;;&lt;BR /&gt; if first.id then do;&lt;BR /&gt; def_ind = 0;&lt;BR /&gt; dt2_first = .;&lt;BR /&gt; cat2_val = '';&lt;BR /&gt; dt3_first = .;&lt;BR /&gt; ghi_ind = 0;&lt;BR /&gt; dt2_ghi = .;&lt;BR /&gt; end;&lt;/P&gt;
&lt;P&gt;* set def and earliest dt2 flag if cat1 set to def and indicator not already set ;&lt;BR /&gt; if cat1 eq 'def' then do;&lt;BR /&gt; if def_ind eq 0 then do;&lt;BR /&gt; def_ind = 1;&lt;BR /&gt; dt2_first = dt2;&lt;BR /&gt; cat2_val = cat2;&lt;BR /&gt; end;&lt;BR /&gt; end;&lt;/P&gt;
&lt;P&gt;* if cat2 same as when cat1 set to def and dt3 set and dt3_first flag not set&amp;nbsp;then set earliest dt3 flag;&lt;BR /&gt; if cat2_val eq cat2 and dt3_first eq . and dt3 ne . then do;&lt;BR /&gt; dt3_first = dt3;&lt;BR /&gt; end;&lt;/P&gt;
&lt;P&gt;* if cat2 same as when cat1 set to def and cat1 set to ghi set ghi indicator and dt2 flag for ghi;;&lt;BR /&gt; if cat2_val eq cat2 and ghi_ind eq 0 and cat1 eq 'ghi' then do;&lt;BR /&gt; ghi_ind = 1;&lt;BR /&gt; dt2_ghi = dt2;&lt;BR /&gt; end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output looks like (sorry, needs aligning):&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; Obs id dt1 dt2 dt3 cat1 cat2 def_ind dt2_first cat2_val dt3_first ghi_ind dt2_ghi&lt;/P&gt;
&lt;P&gt;1 1 18263 17898 . abc a 0 . . 0 .&lt;BR /&gt; 2 1 18263 18287 . def b 1 18287 b . 0 .&lt;BR /&gt; 3 1 18294 17898 . abc a 1 18287 b . 0 .&lt;BR /&gt; 4 1 18294 18287 18296 def b 1 18287 b 18296 0 .&lt;BR /&gt; 5 1 18322 17898 . abc a 1 18287 b 18296 0 .&lt;BR /&gt; 6 1 18322 18333 18333 ghi b 1 18287 b 18296 1 18333&lt;BR /&gt; 7 2 18263 17898 . abc a 0 . . 0 .&lt;BR /&gt; 8 2 18263 18287 . def b 1 18287 b . 0 .&lt;BR /&gt; 9 2 18294 17898 . abc a 1 18287 b . 0 .&lt;BR /&gt; 10 2 18294 18287 18296 def b 1 18287 b 18296 0 .&lt;BR /&gt; 11 2 18322 17898 . abc a 1 18287 b 18296 0 .&lt;BR /&gt; 12 3 18263 17898 . abc a 0 . . 0 .&lt;BR /&gt; 13 3 18263 18287 . def c 1 18287 c . 0 .&lt;BR /&gt; 14 3 18294 17898 . abc a 1 18287 c . 0 .&lt;BR /&gt; 15 3 18294 18287 18296 def b 1 18287 c . 0 .&lt;BR /&gt; 16 3 18322 17898 . abc a 1 18287 c . 0 .&lt;BR /&gt; 17 3 18322 18333 18333 ghi b 1 18287 c . 0 .&lt;BR /&gt; 18 4 18263 17898 . abc a 0 . . 0 .&lt;BR /&gt; 19 4 18263 18287 . def b 1 18287 b . 0 .&lt;BR /&gt; 20 4 18294 17898 . abc a 1 18287 b . 0 .&lt;BR /&gt; 21 4 18294 18287 18296 def b 1 18287 b 18296 0 .&lt;BR /&gt; 22 4 18322 17898 . abc a 1 18287 b 18296 0 .&lt;BR /&gt; 23 4 18322 18333 18333 ghi b 1 18287 b 18296 1 18333&lt;/P&gt;</description>
    <pubDate>Wed, 20 Jan 2016 13:48:58 GMT</pubDate>
    <dc:creator>rivieralad</dc:creator>
    <dc:date>2016-01-20T13:48:58Z</dc:date>
    <item>
      <title>Matching later values of a variable to values as of a given date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-later-values-of-a-variable-to-values-as-of-a-given-date/m-p/244523#M45571</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset at the person-month level (however, each person can have more than one row in one month). It has an ID variable, three&amp;nbsp;date variables, and two categorical variables. If a person has more than one row per month, the values of Date2, Categ 1, and Categ2 will be different in each row.&amp;nbsp;Date1 shows which month/year the variables are "as of".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; Date1 &amp;nbsp; &amp;nbsp; Date2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date3 &amp;nbsp; &amp;nbsp;Categ1 &amp;nbsp; &amp;nbsp; Categ2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;201001 &amp;nbsp; 1/1/2009 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;201001 &amp;nbsp; 1/25/2010 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;201002 &amp;nbsp; 1/1/2009 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;201002 &amp;nbsp; 1/25/2010 &amp;nbsp; 2/3/2010 &amp;nbsp; &amp;nbsp;def &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;201003 &amp;nbsp; 1/1/2009 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; abc &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;201003 &amp;nbsp; 3/12/2010 &amp;nbsp; &amp;nbsp;3/12/2010 &amp;nbsp;ghi &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is to do the following:&lt;/P&gt;&lt;P&gt;1. Create an indicator variable for everyone that has Categ1=def, and a variable that says the earliest Date2 for which they have that value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Take the value of Categ2 at the earliest Date2 where Categ1=def (in this case, b). Find the minimum value of Date3 within the rows where Categ2=b.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. Additionally, also within the rows where Categ2=b, see whether there are any values of ghi in Categ1; if so, create an indicator variable showing that the person had that value and a date variable for when they had it (using Date2).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can do most of this by myself; the issue I'm having is systematizing only looking in the rows with the Categ2 value that corresponds with the earliest instance of Categ1=def.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2016 20:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-later-values-of-a-variable-to-values-as-of-a-given-date/m-p/244523#M45571</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2016-01-19T20:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: Matching later values of a variable to values as of a given date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-later-values-of-a-variable-to-values-as-of-a-given-date/m-p/244668#M45605</link>
      <description>&lt;P&gt;Hi there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming I have understood your problem correctly you will probably want something along these lines (I added some extra ID records to test it).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will need tidying up but this gives you what you asked for in the way of indicators and dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Chris&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt; infile datalines;&lt;BR /&gt; input&lt;BR /&gt; &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/46433"&gt;@01﻿&lt;/a&gt; id $1.&lt;BR /&gt; @08 dt1 yymmn6.&lt;BR /&gt; @17 dt2 mmddyy10.&lt;BR /&gt; &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/29191"&gt;@33﻿&lt;/a&gt; dt3 mmddyy10.&lt;BR /&gt; @43 cat1 $3.&lt;BR /&gt; @54 cat2 $1.&lt;BR /&gt; ;&lt;BR /&gt;datalines;&lt;BR /&gt;1 201001 1/1/2009 . abc a&lt;BR /&gt;1 201001 1/25/2010 . def b&lt;BR /&gt;1 201002 1/1/2009 . abc a&lt;BR /&gt;1 201002 1/25/2010 2/3/2010 def b&lt;BR /&gt;1 201003 1/1/2009 . abc a&lt;BR /&gt;1 201003 3/12/2010 3/12/2010 ghi b&lt;BR /&gt;2 201001 1/1/2009 . abc a&lt;BR /&gt;2 201001 1/25/2010 . def b&lt;BR /&gt;2 201002 1/1/2009 . abc a&lt;BR /&gt;2 201002 1/25/2010 2/3/2010 def b&lt;BR /&gt;2 201003 1/1/2009 . abc a&lt;BR /&gt;3 201001 1/1/2009 . abc a&lt;BR /&gt;3 201001 1/25/2010 . def c&lt;BR /&gt;3 201002 1/1/2009 . abc a&lt;BR /&gt;3 201002 1/25/2010 2/3/2010 def b&lt;BR /&gt;3 201003 1/1/2009 . abc a&lt;BR /&gt;3 201003 3/12/2010 3/12/2010 ghi b&lt;BR /&gt;4 201001 1/1/2009 . abc a&lt;BR /&gt;4 201001 1/25/2010 . def b&lt;BR /&gt;4 201002 1/1/2009 . abc a&lt;BR /&gt;4 201002 1/25/2010 2/3/2010 def b&lt;BR /&gt;4 201003 1/1/2009 . abc a&lt;BR /&gt;4 201003 3/12/2010 3/12/2010 ghi b&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;* sort into each date column order within id (probably don't need to sort by the cat columns);&lt;/P&gt;
&lt;P&gt;proc sort data = have;&lt;BR /&gt; by id dt1 dt2 dt3 cat1 cat2;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;* set flags to meet requirements;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; by id dt1 dt2 dt3 cat1 cat2;&lt;/P&gt;
&lt;P&gt;* retain the indicators and earliest dates;&lt;/P&gt;
&lt;P&gt;retain &lt;BR /&gt; def_ind&lt;BR /&gt; dt2_first&lt;BR /&gt; cat2_val&lt;BR /&gt; dt3_first&lt;BR /&gt; ghi_ind&lt;BR /&gt; dt2_ghi&lt;BR /&gt; ;&lt;/P&gt;
&lt;P&gt;* initialise indicators and earliest dates;;&lt;BR /&gt; if first.id then do;&lt;BR /&gt; def_ind = 0;&lt;BR /&gt; dt2_first = .;&lt;BR /&gt; cat2_val = '';&lt;BR /&gt; dt3_first = .;&lt;BR /&gt; ghi_ind = 0;&lt;BR /&gt; dt2_ghi = .;&lt;BR /&gt; end;&lt;/P&gt;
&lt;P&gt;* set def and earliest dt2 flag if cat1 set to def and indicator not already set ;&lt;BR /&gt; if cat1 eq 'def' then do;&lt;BR /&gt; if def_ind eq 0 then do;&lt;BR /&gt; def_ind = 1;&lt;BR /&gt; dt2_first = dt2;&lt;BR /&gt; cat2_val = cat2;&lt;BR /&gt; end;&lt;BR /&gt; end;&lt;/P&gt;
&lt;P&gt;* if cat2 same as when cat1 set to def and dt3 set and dt3_first flag not set&amp;nbsp;then set earliest dt3 flag;&lt;BR /&gt; if cat2_val eq cat2 and dt3_first eq . and dt3 ne . then do;&lt;BR /&gt; dt3_first = dt3;&lt;BR /&gt; end;&lt;/P&gt;
&lt;P&gt;* if cat2 same as when cat1 set to def and cat1 set to ghi set ghi indicator and dt2 flag for ghi;;&lt;BR /&gt; if cat2_val eq cat2 and ghi_ind eq 0 and cat1 eq 'ghi' then do;&lt;BR /&gt; ghi_ind = 1;&lt;BR /&gt; dt2_ghi = dt2;&lt;BR /&gt; end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output looks like (sorry, needs aligning):&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; Obs id dt1 dt2 dt3 cat1 cat2 def_ind dt2_first cat2_val dt3_first ghi_ind dt2_ghi&lt;/P&gt;
&lt;P&gt;1 1 18263 17898 . abc a 0 . . 0 .&lt;BR /&gt; 2 1 18263 18287 . def b 1 18287 b . 0 .&lt;BR /&gt; 3 1 18294 17898 . abc a 1 18287 b . 0 .&lt;BR /&gt; 4 1 18294 18287 18296 def b 1 18287 b 18296 0 .&lt;BR /&gt; 5 1 18322 17898 . abc a 1 18287 b 18296 0 .&lt;BR /&gt; 6 1 18322 18333 18333 ghi b 1 18287 b 18296 1 18333&lt;BR /&gt; 7 2 18263 17898 . abc a 0 . . 0 .&lt;BR /&gt; 8 2 18263 18287 . def b 1 18287 b . 0 .&lt;BR /&gt; 9 2 18294 17898 . abc a 1 18287 b . 0 .&lt;BR /&gt; 10 2 18294 18287 18296 def b 1 18287 b 18296 0 .&lt;BR /&gt; 11 2 18322 17898 . abc a 1 18287 b 18296 0 .&lt;BR /&gt; 12 3 18263 17898 . abc a 0 . . 0 .&lt;BR /&gt; 13 3 18263 18287 . def c 1 18287 c . 0 .&lt;BR /&gt; 14 3 18294 17898 . abc a 1 18287 c . 0 .&lt;BR /&gt; 15 3 18294 18287 18296 def b 1 18287 c . 0 .&lt;BR /&gt; 16 3 18322 17898 . abc a 1 18287 c . 0 .&lt;BR /&gt; 17 3 18322 18333 18333 ghi b 1 18287 c . 0 .&lt;BR /&gt; 18 4 18263 17898 . abc a 0 . . 0 .&lt;BR /&gt; 19 4 18263 18287 . def b 1 18287 b . 0 .&lt;BR /&gt; 20 4 18294 17898 . abc a 1 18287 b . 0 .&lt;BR /&gt; 21 4 18294 18287 18296 def b 1 18287 b 18296 0 .&lt;BR /&gt; 22 4 18322 17898 . abc a 1 18287 b 18296 0 .&lt;BR /&gt; 23 4 18322 18333 18333 ghi b 1 18287 b 18296 1 18333&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2016 13:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-later-values-of-a-variable-to-values-as-of-a-given-date/m-p/244668#M45605</guid>
      <dc:creator>rivieralad</dc:creator>
      <dc:date>2016-01-20T13:48:58Z</dc:date>
    </item>
  </channel>
</rss>

