<?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: SAS Retain values by two or more groups in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874140#M42871</link>
    <description>&lt;P&gt;So does adding it fix the issue?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update have(obs=0) have;
  by period group_a group_b;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's make a dataset that shows the changed variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data change;
  set have;
  set want(keep=val rename=(val=new_val));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    period        mydate    Group_a    Group_B    Val    new_val

  1    202204    2022-04-01      A1         B1        10       10
  2    202204    2022-04-02      A1         B1        10       10
  3    202204    2022-04-03      A1         B1         .       10
  4    202204    2022-04-04      A1         B1         .       10
  5    202204    2022-04-05      A1         B1         .       10
  6    202204    2022-04-01      A1         B2         .        .
  7    202204    2022-04-02      A1         B2         .        .
  8    202204    2022-04-03      A1         B2        20       20
  9    202204    2022-04-04      A1         B2         .       20
 10    202204    2022-04-05      A1         B2         .       20
 11    202204    2022-04-01      A1         B3         .        .
 12    202204    2022-04-02      A1         B3        40       40
 13    202204    2022-04-03      A1         B3         .       40
 14    202204    2022-04-04      A1         B3        42       42
 15    202204    2022-04-05      A1         B3         .       42
 16    202204    2022-04-01      A2         B1         .        .
 17    202204    2022-04-02      A2         B1         .        .
 18    202204    2022-04-03      A2         B1        15       15
 19    202204    2022-04-04      A2         B1         .       15
 20    202204    2022-04-05      A2         B1         .       15
 21    202205    2022-05-01      A1         B1         .        .
 22    202205    2022-05-02      A1         B1         .        .
 23    202205    2022-05-03      A1         B1        12       12
 24    202205    2022-05-04      A1         B1         .       12
 25    202205    2022-05-05      A1         B1         .       12
 26    202205    2022-05-01      A1         B2         .        .
 27    202205    2022-05-02      A1         B2         .        .
 28    202205    2022-05-03      A1         B2        22       22
 29    202205    2022-05-04      A1         B2         .       22
 30    202205    2022-05-05      A1         B2         .       22
&lt;/PRE&gt;</description>
    <pubDate>Fri, 05 May 2023 14:18:58 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-05-05T14:18:58Z</dc:date>
    <item>
      <title>SAS Retain values by two or more groups</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874114#M42865</link>
      <description>&lt;P&gt;Hi community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need help with retaining values inside two o more groups. All samples I can find are with only one group.&lt;/P&gt;&lt;P&gt;My groups look like:&lt;/P&gt;&lt;P&gt;Group_A : A1, A2&lt;BR /&gt;Group_B : B1, B2, B3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My dataset is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input mydate:ddmmyy10. Group_A $ Group_B $ Val;
format mydate ddmmyy10.;

datalines;
01/04/2022 A1 B1 10
02/04/2022 A1 B1 10
03/04/2022 A1 B1 .
04/04/2022 A1 B1 .
05/04/2022 A1 B1 .
01/04/2022 A1 B2 .
02/04/2022 A1 B2 .
03/04/2022 A1 B2 20
04/04/2022 A1 B2 .
05/04/2022 A1 B2 .
01/04/2022 A1 B3 .
02/04/2022 A1 B3 40
03/04/2022 A1 B3 .
04/04/2022 A1 B3 42
05/04/2022 A1 B3 .
01/04/2022 A2 B1 .
02/04/2022 A2 B1 .
03/04/2022 A2 B1 15
04/04/2022 A2 B1 .
05/04/2022 A2 B1 .
;
run;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;I want the following results:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
input mydate:ddmmyy10. Group_A $ Group_B $ Val;
format mydate ddmmyy10.;

datalines;
01/04/2022 A1 B1 10
02/04/2022 A1 B1 10
03/04/2022 A1 B1 10
04/04/2022 A1 B1 10
05/04/2022 A1 B1 10
01/04/2022 A1 B2 .
02/04/2022 A1 B2 .
03/04/2022 A1 B2 20
04/04/2022 A1 B2 20
05/04/2022 A1 B2 20
01/04/2022 A1 B3 .
02/04/2022 A1 B3 40
03/04/2022 A1 B3 40
04/04/2022 A1 B3 42
05/04/2022 A1 B3 42
01/04/2022 A2 B1 .
02/04/2022 A2 B1 .
03/04/2022 A2 B1 15
04/04/2022 A2 B1 15
05/04/2022 A2 B1 15
;
run;&lt;/PRE&gt;&lt;P&gt;Thanks for your help.&lt;BR /&gt;Regards&lt;/P&gt;</description>
      <pubDate>Fri, 05 May 2023 11:56:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874114#M42865</guid>
      <dc:creator>MM88</dc:creator>
      <dc:date>2023-05-05T11:56:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Retain values by two or more groups</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874116#M42866</link>
      <description>&lt;P&gt;If you found examples for one group, then it should be relatively simple to change it to 2 or more groups&lt;BR /&gt;&lt;BR /&gt;I recommend you review&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/lrcon/9.4/n138da4gme3zb7n1nifpfhqv7clq.htm" target="_self"&gt;BY-Group Processing in the DATA Step&lt;/A&gt;&amp;nbsp;&lt;BR /&gt;Here's an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input mydate:ddmmyy10. Group_A $ Group_B $ Val;
format mydate ddmmyy10.;

datalines;
01/04/2022 A1 B1 10
02/04/2022 A1 B1 10
03/04/2022 A1 B1 .
04/04/2022 A1 B1 .
05/04/2022 A1 B1 .
01/04/2022 A1 B2 .
02/04/2022 A1 B2 .
03/04/2022 A1 B2 20
04/04/2022 A1 B2 .
05/04/2022 A1 B2 .
01/04/2022 A1 B3 .
02/04/2022 A1 B3 40
03/04/2022 A1 B3 .
04/04/2022 A1 B3 42
05/04/2022 A1 B3 .
01/04/2022 A2 B1 .
02/04/2022 A2 B1 .
03/04/2022 A2 B1 15
04/04/2022 A2 B1 .
05/04/2022 A2 B1 .
;
run;

proc sort 
		data=work.have 
		out=work.srtdHave ;
	by Group_A Group_B mydate ;
run ;

data work.want (drop=value) ;	
	retain value . ;
	set srtdHave ;
	by Group_A Group_B mydate ;
	if first.Group_B then 
		value=. ;
	if val ne . then 
		value=val ;
	else
		val=value ;
	output work.want ;
run ;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 May 2023 12:09:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874116#M42866</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2023-05-05T12:09:20Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Retain values by two or more groups</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874133#M42867</link>
      <description>&lt;P&gt;Looks like a simple LOCF (last observation carried forward).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can implement that with the UPDATE statement.&amp;nbsp; Use the source dataset as both the BASE and the TRANSACTION data. Add an OUTPUT statement so that an observation is written after every transaction is applied instead of the normal processing which would only write one observation per BY group that had all of the transactions for it applied.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update have(obs=0) have;
   by group_a group_b;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 May 2023 13:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874133#M42867</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-05T13:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Retain values by two or more groups</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874135#M42868</link>
      <description>&lt;P&gt;Hi experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your reply. I was trying to apply the solutions to my problem, but I have more variables. Please find a modified 'have' dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input period mydate:ddmmyy10. Group_a $ Group_B $ Val;
format mydate ddmmyy10.;

datalines;
202204 01/04/2022 A1 B1 10
202204 02/04/2022 A1 B1 10
202204 03/04/2022 A1 B1 .
202204 04/04/2022 A1 B1 .
202204 05/04/2022 A1 B1 .
202204 01/04/2022 A1 B2 .
202204 02/04/2022 A1 B2 .
202204 03/04/2022 A1 B2 20
202204 04/04/2022 A1 B2 .
202204 05/04/2022 A1 B2 .
202204 01/04/2022 A1 B3 .
202204 02/04/2022 A1 B3 40
202204 03/04/2022 A1 B3 .
202204 04/04/2022 A1 B3 42
202204 05/04/2022 A1 B3 .
202204 01/04/2022 A2 B1 .
202204 02/04/2022 A2 B1 .
202204 03/04/2022 A2 B1 15
202204 04/04/2022 A2 B1 .
202204 05/04/2022 A2 B1 .
202205 01/05/2022 A1 B1 .
202205 02/05/2022 A1 B1 .
202205 03/05/2022 A1 B1 12
202205 04/05/2022 A1 B1 .
202205 05/05/2022 A1 B1 .
202205 01/05/2022 A1 B2 .
202205 02/05/2022 A1 B2 .
202205 03/05/2022 A1 B2 22
202205 04/05/2022 A1 B2 .
202205 05/05/2022 A1 B2 .

;
run;&lt;/PRE&gt;&lt;P&gt;The generated dataset has some incorrect val, for example at line:&lt;/P&gt;&lt;P&gt;period: 202205&lt;/P&gt;&lt;P&gt;mydate: 01/05/2022&lt;/P&gt;&lt;P&gt;group_A: A1&lt;/P&gt;&lt;P&gt;group_B: B1&lt;/P&gt;&lt;P&gt;Val: 10 (It must be missing, according to 'have' dataset).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the suggestions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 May 2023 14:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874135#M42868</guid>
      <dc:creator>MM88</dc:creator>
      <dc:date>2023-05-05T14:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Retain values by two or more groups</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874137#M42869</link>
      <description>&lt;P&gt;So PERIOD is one of the BY variables also?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 May 2023 14:16:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874137#M42869</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-05T14:16:33Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Retain values by two or more groups</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874139#M42870</link>
      <description>Yes, PERIOD is also a BY variable.</description>
      <pubDate>Fri, 05 May 2023 14:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874139#M42870</guid>
      <dc:creator>MM88</dc:creator>
      <dc:date>2023-05-05T14:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Retain values by two or more groups</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874140#M42871</link>
      <description>&lt;P&gt;So does adding it fix the issue?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update have(obs=0) have;
  by period group_a group_b;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's make a dataset that shows the changed variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data change;
  set have;
  set want(keep=val rename=(val=new_val));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    period        mydate    Group_a    Group_B    Val    new_val

  1    202204    2022-04-01      A1         B1        10       10
  2    202204    2022-04-02      A1         B1        10       10
  3    202204    2022-04-03      A1         B1         .       10
  4    202204    2022-04-04      A1         B1         .       10
  5    202204    2022-04-05      A1         B1         .       10
  6    202204    2022-04-01      A1         B2         .        .
  7    202204    2022-04-02      A1         B2         .        .
  8    202204    2022-04-03      A1         B2        20       20
  9    202204    2022-04-04      A1         B2         .       20
 10    202204    2022-04-05      A1         B2         .       20
 11    202204    2022-04-01      A1         B3         .        .
 12    202204    2022-04-02      A1         B3        40       40
 13    202204    2022-04-03      A1         B3         .       40
 14    202204    2022-04-04      A1         B3        42       42
 15    202204    2022-04-05      A1         B3         .       42
 16    202204    2022-04-01      A2         B1         .        .
 17    202204    2022-04-02      A2         B1         .        .
 18    202204    2022-04-03      A2         B1        15       15
 19    202204    2022-04-04      A2         B1         .       15
 20    202204    2022-04-05      A2         B1         .       15
 21    202205    2022-05-01      A1         B1         .        .
 22    202205    2022-05-02      A1         B1         .        .
 23    202205    2022-05-03      A1         B1        12       12
 24    202205    2022-05-04      A1         B1         .       12
 25    202205    2022-05-05      A1         B1         .       12
 26    202205    2022-05-01      A1         B2         .        .
 27    202205    2022-05-02      A1         B2         .        .
 28    202205    2022-05-03      A1         B2        22       22
 29    202205    2022-05-04      A1         B2         .       22
 30    202205    2022-05-05      A1         B2         .       22
&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 May 2023 14:18:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-Retain-values-by-two-or-more-groups/m-p/874140#M42871</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-05T14:18:58Z</dc:date>
    </item>
  </channel>
</rss>

