<?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: COUNTER, RETAIN AND FIRST. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601116#M76367</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/297746"&gt;@ebeth&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This worked almost perfectly. Thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One odd thing though is that the very first NUM_A is a missing value. The only missing value.&amp;nbsp; Instead of zero. I thought the RETAIN statement set that to zero?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Good catch. The RETAIN statement just retains values instead of setting them to missing before reading in a new line of data.&lt;/P&gt;
&lt;P&gt;What's happening here: The first value of B is not missing so here the logic for the value in the first observation leads to the else case: NUM_B=NUM_B+1;&lt;/P&gt;
&lt;P&gt;Because here variable NUM_B is still missing, the formula sums the values &amp;lt;missing&amp;gt;+1. This results is a missing.&lt;/P&gt;
&lt;P&gt;To avoid such a case: Either set all the values to 0 whenever there is a new id (if first.id) or even simpler use the sum() function as done below. The sum() function ignores missings and though the result of sum(&amp;lt;missing&amp;gt;,1) is 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below code demonstrates 3 different coding options all returning the same result.&lt;/P&gt;
&lt;P&gt;Call missing() allows to set an array of variables to missing. There is unfortunately still no call routine which would allow to set variables to another value; except for a not very often used "hack" using call stdize()&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create desired result */
data want;
  set have;
  by id;
  retain NUM_A NUM_B;
  if first.id then 
    do;
      NUM_A=0;
      call missing(NUM_B, NUM_C);
      /** alternatively use below syntax to set the variables to zero
      call stdize('replace','mult=',0,'none',NUM_A,NUM_B,NUM_C);
      **/
    end;
  if missing(a) then NUM_A=0; else NUM_A=NUM_A+1;
  if missing(b) then NUM_B=0; else NUM_B=sum(NUM_B,1);
  /* using syntax NUM_C+1 implicitly retains the variable */
  if missing(c) then NUM_C=0; else NUM_C+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;Also note that if the syntax to calculate NUM_C then SAS not only retains the variable implicitly but it also behaves the same like when using the sum() function.&lt;/P&gt;</description>
    <pubDate>Sat, 02 Nov 2019 01:52:02 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2019-11-02T01:52:02Z</dc:date>
    <item>
      <title>COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/600857#M76346</link>
      <description>&lt;P&gt;Hello, I'm a 2 month old SAS user and just started practicing COUNTER, RETAIN, FIRST. ,Last. and DO/END. I can&amp;nbsp; understand simple examples (of course) but am getting stuck when there is missing data and multiple variables involved.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;This data is sorted BY ID DATE TYPE already and the columns in red are what I'm trying to add in.&lt;/P&gt;&lt;P&gt;I think Num_A should be counter+1 when the variable is the first non=missing value for A and then start over for each date. And eventually if the ID changed as well. The same for num_B and C.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&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t;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;1&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3/17/2010&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;1&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5/1/2010&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;1&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5/1/2010&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;1&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5/1/2010&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;0&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;1&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 23:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/600857#M76346</guid>
      <dc:creator>ebeth</dc:creator>
      <dc:date>2019-10-31T23:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/600858#M76347</link>
      <description>&lt;P&gt;You need explain what your red columns mean, as clearly as you can.&lt;/P&gt;
&lt;P&gt;Note your words mention COUNTER, but there is no such variable shown, Either as input or as one to be derived.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also your data is sorted by ID and DATE , but not by TYPE.&amp;nbsp; How was the order among the rows with the same values of ID and DATE set?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 23:39:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/600858#M76347</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-31T23:39:30Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/600873#M76348</link>
      <description>&lt;P&gt;Below a coding option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create sample data */
data have;
  infile datalines truncover dlm='|';
  input ID DATE:mmddyy10. TYPE $ A B C _NUM_A _NUM_B _NUM_C;
  format date date9.;
  datalines;
1|1/1/2010|B|.|10|.|0|1|0
1|1/1/2010|A|3|.|.|1|0|0
1|1/1/2010|A|7|.|.|2|0|0
1|1/1/2010|C|.|.|11|0|0|1
1|3/17/2010|B|.|5|.|0|1|0
1|3/17/2010|A|4|.|.|1|0|0
1|3/17/2010|C|.|.|3|0|0|1
1|5/1/2010|B|.|7|.|0|1|0
1|5/1/2010|A|3|.|.|1|0|0
1|5/1/2010|C|.|.|4|0|0|
;

/* create desired result */
data want;
  set have;
  by id;
  retain NUM_A NUM_B;
  if first.id then call missing(NUM_A, NUM_B, NUM_C);
  if missing(a) then NUM_A=0; else NUM_A=NUM_A+1;
  if missing(b) then NUM_B=0; else NUM_B=NUM_B+1;
  /* using syntax NUM_C+1 implicitly retains the variable */
  if missing(c) then NUM_C=0; else NUM_C+1;
run;

title 'Want';
proc print data=want;
run;
title;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Nov 2019 01:38:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/600873#M76348</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-11-02T01:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/600995#M76360</link>
      <description>&lt;P&gt;The very first thing you will need to explain is the sort order. Since to use FIRST. there must be a BY statement, then please at least share the BY statement you are using.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 15:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/600995#M76360</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-01T15:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601066#M76362</link>
      <description>&lt;P&gt;I used PROC SORT to sort BY ID DATE (must have forgotten TYPE). If I do sort by TYPE as well though, my goal is to add in the 3 columns in red. The red columns are not part of the original data; I want to create those counts.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, NUM_A should count "1", then&amp;nbsp; "2" for the first 2 A's on the same date and ID.&amp;nbsp; or 3 if there is a 3rd A for that date and ID Then start the count over for the next A on a different date, but same ID.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 19:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601066#M76362</guid>
      <dc:creator>ebeth</dc:creator>
      <dc:date>2019-11-01T19:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601070#M76363</link>
      <description>&lt;P&gt;So you want running count of NON missing values of A, B and C?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id date type;
  if first.date then do;
    num_a=0;
    num_b=0;
    num_c=0;
  end;
  num_a +  not missing(A);
  num_b +  not missing(B);
  num_c +  not missing(C);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to count number of distinct values of A (or B or C) then hat will be harder.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 19:20:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601070#M76363</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-01T19:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601071#M76364</link>
      <description>&lt;P&gt;This worked almost perfectly. Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;One odd thing though is that the very first NUM_A is a missing value. The only missing value.&amp;nbsp; Instead of zero. I thought the RETAIN statement set that to zero?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 19:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601071#M76364</guid>
      <dc:creator>ebeth</dc:creator>
      <dc:date>2019-11-01T19:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601089#M76365</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Yes, I want running count of non missing values of A, B and C.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works perfectly for NUM_B and NUM_C.&lt;/P&gt;&lt;P&gt;But NUM_A&amp;nbsp; is showing a "1" for every line on the same date. Then a 0 for the next new date. Even when there is a missing value of A it outputs 1 until new date.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 20:40:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601089#M76365</guid>
      <dc:creator>ebeth</dc:creator>
      <dc:date>2019-11-01T20:40:08Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601113#M76366</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/297746"&gt;@ebeth&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This worked almost perfectly. Thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One odd thing though is that the very first NUM_A is a missing value. The only missing value.&amp;nbsp; Instead of zero. I thought the RETAIN statement set that to zero?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Retain can optionally assign a starting value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retain varname 0;&lt;/P&gt;
&lt;P&gt;If no value is specified it will be missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would have to show your actual code for us to see if there was something other cause of a missing value.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Nov 2019 22:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601113#M76366</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-01T22:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601116#M76367</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/297746"&gt;@ebeth&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This worked almost perfectly. Thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One odd thing though is that the very first NUM_A is a missing value. The only missing value.&amp;nbsp; Instead of zero. I thought the RETAIN statement set that to zero?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Good catch. The RETAIN statement just retains values instead of setting them to missing before reading in a new line of data.&lt;/P&gt;
&lt;P&gt;What's happening here: The first value of B is not missing so here the logic for the value in the first observation leads to the else case: NUM_B=NUM_B+1;&lt;/P&gt;
&lt;P&gt;Because here variable NUM_B is still missing, the formula sums the values &amp;lt;missing&amp;gt;+1. This results is a missing.&lt;/P&gt;
&lt;P&gt;To avoid such a case: Either set all the values to 0 whenever there is a new id (if first.id) or even simpler use the sum() function as done below. The sum() function ignores missings and though the result of sum(&amp;lt;missing&amp;gt;,1) is 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below code demonstrates 3 different coding options all returning the same result.&lt;/P&gt;
&lt;P&gt;Call missing() allows to set an array of variables to missing. There is unfortunately still no call routine which would allow to set variables to another value; except for a not very often used "hack" using call stdize()&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create desired result */
data want;
  set have;
  by id;
  retain NUM_A NUM_B;
  if first.id then 
    do;
      NUM_A=0;
      call missing(NUM_B, NUM_C);
      /** alternatively use below syntax to set the variables to zero
      call stdize('replace','mult=',0,'none',NUM_A,NUM_B,NUM_C);
      **/
    end;
  if missing(a) then NUM_A=0; else NUM_A=NUM_A+1;
  if missing(b) then NUM_B=0; else NUM_B=sum(NUM_B,1);
  /* using syntax NUM_C+1 implicitly retains the variable */
  if missing(c) then NUM_C=0; else NUM_C+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;Also note that if the syntax to calculate NUM_C then SAS not only retains the variable implicitly but it also behaves the same like when using the sum() function.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Nov 2019 01:52:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601116#M76367</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-11-02T01:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601117#M76368</link>
      <description>&lt;P&gt;Show the example data that is not producing expected results.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also from your original example it looks like the A,B and C variables are not really needed as you could just use the value of TYPE instead to tell which variable to increment.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Nov 2019 01:31:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601117#M76368</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-02T01:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: COUNTER, RETAIN AND FIRST.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601168#M76376</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Sat, 02 Nov 2019 20:17:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/COUNTER-RETAIN-AND-FIRST/m-p/601168#M76376</guid>
      <dc:creator>ebeth</dc:creator>
      <dc:date>2019-11-02T20:17:18Z</dc:date>
    </item>
  </channel>
</rss>

