<?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 statement with two by group variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617007#M180735</link>
    <description>thank you! I'm not familiar with lag functions at all so will check this out.</description>
    <pubDate>Mon, 13 Jan 2020 18:55:35 GMT</pubDate>
    <dc:creator>adventuresinsas</dc:creator>
    <dc:date>2020-01-13T18:55:35Z</dc:date>
    <item>
      <title>Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/616987#M180726</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I just can't seem to figure this out so hoping that someone here can help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this dataset, where Var1 is an indicator for a particular event:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; Month&amp;nbsp; Var1&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp;&amp;nbsp;Jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp; Jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp; Feb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; Jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; Jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; Feb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; Mar&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a new dataset that contains a new count variable that will count the number in Var1 for each ID/month group so that it looks like this:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; Month&amp;nbsp; Var1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Count&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp;&amp;nbsp;Jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp; Jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp; Feb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; Jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; Jan&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; Feb&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; Mar&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I do this with a retain statement? This is what I tried:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	where var1=1;
	by id month;
	if first.month then Count=0;
		Count+1;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(Side note: I know this probably won't give me the row where Var1=. but that's okay.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 17:43:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/616987#M180726</guid>
      <dc:creator>adventuresinsas</dc:creator>
      <dc:date>2020-01-13T17:43:06Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/616992#M180728</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $  Month $  Var1;
cards;
A   Jan       1
A   Jan       1
A   Feb       .
B   Jan       1
B   Jan       1
B   Feb      1
B   Mar      1
;

data want;
 set have;
 by id month notsorted;
 if first.month and var1 then count=1;
 else if var1 then count+1;
 else count=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;/*Or just*/&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 by id month notsorted;
 if first.month then count=.;
 if var1 then count+1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;/*Or*/&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 by id month var1 notsorted;
 if first.var1 then count=.;
 if var1 then count+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 18:09:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/616992#M180728</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-13T18:09:32Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/616993#M180729</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282435"&gt;@adventuresinsas&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to specify "notsorted" in the by statement as the value are considered as character values (not a format of a numeric variable for the month 1-12). So SAS consider they are not sorted properly (Feb should occur before Jan etc. alphabetically)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 by id month notsorted;
 if first.month and var1 then count=1;
 else if var1 then count+1;
 else count=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Jan 2020 18:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/616993#M180729</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-01-13T18:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/616994#M180730</link>
      <description>&lt;P&gt;Oh, okay. So I'm sorry, I should have specified but month is actually in YYMM7. format (so it displays as 2018M10, for instance). I did do a proc sort by ID and month. But I will give this notsorted option a try as well.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 18:15:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/616994#M180730</guid>
      <dc:creator>adventuresinsas</dc:creator>
      <dc:date>2020-01-13T18:15:26Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617001#M180732</link>
      <description>&lt;P&gt;So what was wrong with the results you got from running your code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 18:31:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617001#M180732</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-13T18:31:28Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617005#M180733</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282435"&gt;@adventuresinsas&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case it is easier to use Lag functions instead of by variables with notsorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $  Month $  Var1;
cards;
A   Jan       1
A   Jan       1
A   Feb       .
B   Jan       1
B   Jan       1
B   Feb      1
B   Mar      1
;

data want; set have;
	if lag(ID)=ID and lag(Month)=Month then count + 1; 
	else count=Var1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="count.gif" style="width: 370px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35306i9B7DC8C63FB1FB48/image-size/large?v=v2&amp;amp;px=999" role="button" title="count.gif" alt="count.gif" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 18:52:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617005#M180733</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-01-13T18:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617006#M180734</link>
      <description>&lt;P&gt;I just ran it again. I have another variable in my dataset for exact date and it looks like my code only counts observations occurring on the same date (as though the bygroup is date). Which doesn't make sense to me because I don't list the date variable anywhere in my code for proc sort or the data step. So in the end, I could have some observations occurring in the same month but the count gets reset whenever the actual date changes, not the month.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 18:52:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617006#M180734</guid>
      <dc:creator>adventuresinsas</dc:creator>
      <dc:date>2020-01-13T18:52:25Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617007#M180735</link>
      <description>thank you! I'm not familiar with lag functions at all so will check this out.</description>
      <pubDate>Mon, 13 Jan 2020 18:55:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617007#M180735</guid>
      <dc:creator>adventuresinsas</dc:creator>
      <dc:date>2020-01-13T18:55:35Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617011#M180737</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282435"&gt;@adventuresinsas&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the Month variable is a SAS date. It is formatted to appear as a month value, but it contains a numeric value counting days from jan 1, 1960. And the value is different, if the days are different, even if they are in the same month. Here is a modified code to handle that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID$ Month date9. Var1;
format Month YYMM7.;
cards;
A 12jan2019 1
A 20jan2019 1
A 16feb2019 .
B 01jan2019 1
B 17jan2019 1
B 24feb2019 1
B 07mar2019 1
;

data want; set have;
	if lag(ID)=ID and month(lag(Month))=month(Month) then count + 1; 
	else count=Var1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result - same as previous mail.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 19:03:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617011#M180737</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-01-13T19:03:58Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617014#M180738</link>
      <description>&lt;P&gt;When you attach a format to a variable it changes how SAS displays the value, but it does not change the actual value.&amp;nbsp; So probably to get the behavior you are seeing the two variables have the same values, but one is displaying just the month and the other displays the day of the month also.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some procedures, like PROC FREQ and PROC MEANS, will use the formatted value when aggregating by a variable.&amp;nbsp; But the BY processing of the DATA step looks at the actual values to see when they change, not the formatted (display) values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could fix that by converting the variable that you are using to show the month to use the same day of the month every time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So something like this will make a copy of a date variable named DATE into another date valued variable named MONTH, but force the values to the first day of the month.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  month =intnx('month',date,0,'b');
  format month yymm7. date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 19:06:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617014#M180738</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-13T19:06:05Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement with two by group variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617017#M180739</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/282435"&gt;@adventuresinsas&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I realize it cannot handle a situation, where two consecutive records for the same ID has the same month, but in different years. They will be seen as the same month. This should be failsafe:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want; set have;
	if lag(ID)=ID and put(lag(Month),YYMM7.)=put(Month,YYMM7.) then count + 1; 
	else count=Var1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2020 19:11:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-with-two-by-group-variables/m-p/617017#M180739</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-01-13T19:11:52Z</dc:date>
    </item>
  </channel>
</rss>

