<?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: Retain by two variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829304#M327650</link>
    <description>&lt;P&gt;There are probably more elegant ways of doing this but SQL works for the data you've supplied:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as

  select A.ID
        ,A.DATE
        ,coalesce(A.VAR1, B.Max_VAR1) as VAR1 
        ,coalesce(A.VAR2, B.Max_VAR2) as VAR2
        ,coalesce(A.VAR3, B.Max_VAR3) as VAR3
  from have as A

  left join
  (select ID
          ,DATE
          ,max(VAR1) as Max_VAR1
          ,max(VAR2) as Max_VAR2
          ,max(VAR3) as Max_VAR3
   from have
   group by ID
           ,DATE
  ) as B
  on A.ID = B.ID
  and A.DATE = B.DATE
  order by A.ID
          ,A.DATE 
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 19 Aug 2022 02:17:26 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2022-08-19T02:17:26Z</dc:date>
    <item>
      <title>Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829291#M327639</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;I have a data set that looks like the following:&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;DATE&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;VAR1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;VAR2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;VAR3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;8-22-2022&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;a&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;8-22-2022&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;b&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;7-23-2021&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;c&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;11-12-2019&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;a&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;10-14-2019&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;c&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID DATE :mmddyy10. VAR1 $ VAR2 $ VAR3 $ ;
format DATE mmddyy10.;
datalines;
1 08-22-2022 a . .
1 08-22-2022 . b .
1 07-23-2021 . . c
2 11-12-2019 a . .
2 10-14-2019 . . c
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;I want something that looks like the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;DATE&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;VAR1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;VAR2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;VAR3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;8-22-2022&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;a&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;b&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;8-22-2022&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;a&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;b&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;7-23-2021&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;c&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;11-12-2019&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;a&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;10-14-2019&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;c&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input ID DATE :mmddyy10. VAR1 $ VAR2 $ VAR3 $ ;
format DATE mmddyy10.;
datalines;
1 08-22-2022 a b .
1 08-22-2022 a b .
1 07-23-2021 . . c
2 11-12-2019 a . .
2 10-14-2019 . . c
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Essentially, I need a data set where the values for var1 - var3 are retained if the id AND the date are the same. I tried something like this; however, it did not work (also other similar iterations of the code below (with first.id and first.date).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data test;
set have;
by id date;
retain met_ihc2ln_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Thank you in advance for any help you can provide!&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 01:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829291#M327639</guid>
      <dc:creator>awardell</dc:creator>
      <dc:date>2022-08-19T01:40:51Z</dc:date>
    </item>
    <item>
      <title>Re: Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829294#M327642</link>
      <description>&lt;P&gt;Since your date is missing on the first example record how does that meet the requirement "the id AND the date are the same"? So you need to describe what is going on when a date obviously doesn't match that it should be processed this way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retain won't work directly because it can keep a value from a PREVIOUS record, not look ahead to the NEXT record as you are doing in a couple of places.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 21:41:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829294#M327642</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-18T21:41:16Z</dc:date>
    </item>
    <item>
      <title>Re: Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829297#M327645</link>
      <description>&lt;P&gt;I see what you are saying. I had made a mistake with the date being missing in my first chart, no dates should be missing. Do you know of a way that I could look to the NEXT record across date and id? I am still stuck with how to get this.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 01:41:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829297#M327645</guid>
      <dc:creator>awardell</dc:creator>
      <dc:date>2022-08-19T01:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829304#M327650</link>
      <description>&lt;P&gt;There are probably more elegant ways of doing this but SQL works for the data you've supplied:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as

  select A.ID
        ,A.DATE
        ,coalesce(A.VAR1, B.Max_VAR1) as VAR1 
        ,coalesce(A.VAR2, B.Max_VAR2) as VAR2
        ,coalesce(A.VAR3, B.Max_VAR3) as VAR3
  from have as A

  left join
  (select ID
          ,DATE
          ,max(VAR1) as Max_VAR1
          ,max(VAR2) as Max_VAR2
          ,max(VAR3) as Max_VAR3
   from have
   group by ID
           ,DATE
  ) as B
  on A.ID = B.ID
  and A.DATE = B.DATE
  order by A.ID
          ,A.DATE 
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Aug 2022 02:17:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829304#M327650</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-08-19T02:17:26Z</dc:date>
    </item>
    <item>
      <title>Re: Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829308#M327651</link>
      <description>&lt;P&gt;Just another solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   length v1-v3 $ 1;
   
   do _n_ = 1 by 1 until(last.date);
      set have;
      by id date notsorted;
      v1 = coalescec(v1, var1);
      v2 = coalescec(v2, var2);
      v3 = coalescec(v3, var3);
   end;
   
   do _n_ = 1 by 1 until(last.date);
      set have;
      by id date notsorted;
      var1 = coalescec(v1, var1);
      var2 = coalescec(v2, var2);
      var3 = coalescec(v3, var3);
   end;
   
   drop v1-v3;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Aug 2022 05:10:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829308#M327651</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-08-19T05:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829346#M327671</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID DATE :mmddyy10. VAR1 $ VAR2 $ VAR3 $ ;
format DATE mmddyy10.;
datalines;
1 08-22-2022 a . .
1 08-22-2022 . b .
1 07-23-2021 . . c
2 11-12-2019 a . .
2 10-14-2019 . . c
;
run;


proc sql;
create table want(drop=dummy) as
select ID,DATE,var1 as dummy,max(var1) as var1,max(var2) as var2,max(var3) as var3
 from have 
  group by ID,DATE ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Aug 2022 12:41:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829346#M327671</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-19T12:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829355#M327674</link>
      <description>&lt;P&gt;Your example data is not complex enough to actually test all possible scenarios.&amp;nbsp; In particular, what do you want to do when there is more than one non-missing value for a variable within a group?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is method using the UPDATE statement.&amp;nbsp; First collapse to final values per group by using the existing data as transactions against an empty dataset.&amp;nbsp; Then reapply the transactions to get the detailed observations written out.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input ID DATE :mmddyy. (VAR1-VAR3) (:$8.);
  format date yymmdd10.;
cards;
1 8-22-2022 a . .    
1 8-22-2022 . b . 
1 7-23-2021 . . c
2 11-12-2019 a . .  
2 10-14-2019 . . c
;

data step1;
  update have(obs=0) have ;
  by id descending date;
run;

data want ;
  update step1 have;
  by id descending date;
  output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    ID          DATE    VAR1    VAR2    VAR3

 1      1    2022-08-22     a       b
 2      1    2022-08-22     a       b
 3      1    2021-07-23                     c
 4      2    2019-11-12     a
 5      2    2019-10-14                     c

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 13:38:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829355#M327674</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-19T13:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829405#M327697</link>
      <description>&lt;P&gt;Thank you so much! This worked great! I appreciate all of your help!&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 18:17:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829405#M327697</guid>
      <dc:creator>awardell</dc:creator>
      <dc:date>2022-08-19T18:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829406#M327698</link>
      <description>&lt;P&gt;Thank you so much for your help! I have never seen the coalesce function used like this, and I appreciate it!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 18:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829406#M327698</guid>
      <dc:creator>awardell</dc:creator>
      <dc:date>2022-08-19T18:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: Retain by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829407#M327699</link>
      <description>&lt;P&gt;Thank you so much for this solution! I appreciate your help!&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 18:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-by-two-variables/m-p/829407#M327699</guid>
      <dc:creator>awardell</dc:creator>
      <dc:date>2022-08-19T18:21:09Z</dc:date>
    </item>
  </channel>
</rss>

