<?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: Count the number of non-missing values in a variable, by ID? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/628503#M185736</link>
    <description>&lt;P&gt;Thanks, this is exactly what I was looking for. I had the logic down, but I was trying to condition "if first.ID" as "if first.var1".&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 01 Mar 2020 01:21:47 GMT</pubDate>
    <dc:creator>UniversitySas</dc:creator>
    <dc:date>2020-03-01T01:21:47Z</dc:date>
    <item>
      <title>Count the number of non-missing values in a variable, by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626764#M184892</link>
      <description>&lt;P&gt;Suppose I have some data with three variables:&lt;/P&gt;&lt;P&gt;ID, Date, Var1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a 'counter' variable for Var1, such that for each ID, every time Var1 is non-empty/non-missing, my var1_counter increases by 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output would look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
infile DATALINES dsd missover;
input ID DATE(month_num) Var1 Var1_Counter;
CARDS;
01, 1, ., .
01, 2, 3.5, 1
01, 3, ., 1
01, 4, 1.4, 2
01, 5, ., 2
02, 1, ., .
02, 2, 1.2, 1
02, 3, 1.4, 2
02, 4, 1.7, 3
02, 5, 5.1, 4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Additionally, if I could just get the final count for each ID, (in my example final count for ID = 01 would be 2, for 02 it would be 4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 00:04:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626764#M184892</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-02-24T00:04:08Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of non-missing values in a variable, by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626766#M184893</link>
      <description>&lt;P&gt;I would suggest using PROC MEANS if the variable is numeric. If its character you'll need a different approach via PROC FREQ.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have N NMISS;
class id;
var var1;
ods output summary=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output will be displayed and in the data set called WANT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95638"&gt;@UniversitySas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Suppose I have some data with three variables:&lt;/P&gt;
&lt;P&gt;ID, Date, Var1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create a 'counter' variable for Var1, such that for each ID, every time Var1 is non-empty/non-missing, my var1_counter increases by 1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The output would look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
infile DATALINES dsd missover;
input ID DATE(month_num) Var1 Var1_Counter;
CARDS;
01, 1, ., .
01, 2, 3.5, 1
01, 3, ., 1
01, 4, 1.4, 2
01, 5, ., 2
02, 1, ., .
02, 2, 1.2, 1
02, 3, 1.4, 2
02, 4, 1.7, 3
02, 5, 5.1, 4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Additionally, if I could just get the final count for each ID, (in my example final count for ID = 01 would be 2, for 02 it would be 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 00:58:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626766#M184893</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-24T00:58:35Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of non-missing values in a variable, by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626775#M184896</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set HAVE;
  by ID;
  if first.ID then COUNTER=0;
  COUNTER+(VAR1 ne .);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 02:09:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626775#M184896</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-02-24T02:09:20Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of non-missing values in a variable, by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626824#M184927</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95638"&gt;@UniversitySas&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A complementary approach to the one proposed by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;, using PROC SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	/* create table want as */
	select count(Var1) as Not_Missing,
		   nmiss(Var1) as Missing
	from temp
	group by ID;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to output the results of PROC MEANS:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=temp n nmiss;
	var Var1;
	class id;
	output out=want n= nmiss= / autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 09:26:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626824#M184927</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-24T09:26:33Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of non-missing values in a variable, by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626858#M184941</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2(drop=Counter);
  set temp;

  attrib Counter length=8;

  by ID;

  if first.ID then Counter = 0;

  if Var1 ne . then Counter+1; * retains Counter and adds 1;

  if Counter = 0 then Var1_Counter = .;
  else Var1_Counter = Counter;

  if last.ID then output; * use this if you only need totals;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Feb 2020 13:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626858#M184941</guid>
      <dc:creator>nicobuettner</dc:creator>
      <dc:date>2020-02-24T13:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of non-missing values in a variable, by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626860#M184942</link>
      <description>&lt;P&gt;Do you really want the counter to start with MISSING instead of ZERO?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set temp;
  by id;
  if first.id then want=0;
  want + not missing(var1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;             month_             Var1_
Obs    ID      num     Var1    Counter    want

  1     1       1        .        .         0
  2     1       2       3.5       1         1
  3     1       3        .        1         1
  4     1       4       1.4       2         2
  5     1       5        .        2         2
  6     2       1        .        .         0
  7     2       2       1.2       1         1
  8     2       3       1.4       2         2
  9     2       4       1.7       3         3
 10     2       5       5.1       4         4
&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Feb 2020 13:36:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/626860#M184942</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-24T13:36:11Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of non-missing values in a variable, by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/628503#M185736</link>
      <description>&lt;P&gt;Thanks, this is exactly what I was looking for. I had the logic down, but I was trying to condition "if first.ID" as "if first.var1".&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Mar 2020 01:21:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-non-missing-values-in-a-variable-by-ID/m-p/628503#M185736</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-03-01T01:21:47Z</dc:date>
    </item>
  </channel>
</rss>

