<?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 obervations by id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/710904#M218917</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input id $ date mmddyy10. disease @@;
format date mmddyy10.;
datalines;
1 01/08/2016 1
1 01/08/2016 1
1 01/10/2016 .
1 01/11/2016 1
2 01/13/2016 1
2 04/04/2016 1
2 06/05/2016 1
3 12/16/2016 .
3 11/17/2016 .
4 05/18/2016 1
4 05/18/2016 1
4 05/18/2016 1
4 03/16/2016 .
5 04/10/2016 .
5 04/16/2016 1
;
run;

data want;
 set have;
 by id date notsorted;
 if first.id then count=0;
 if first.date then count+disease;
 if last.id and count;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 12 Jan 2021 19:12:15 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2021-01-12T19:12:15Z</dc:date>
    <item>
      <title>Count obervations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/710901#M218915</link>
      <description>&lt;P&gt;Hi expert,&lt;/P&gt;&lt;P&gt;I wanted to count the 'disease' variable but need to avoid misisng 'disease' and duplicate dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input id $ date mmddyy10. disease @@;&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 01/08/2016 1&lt;BR /&gt;1 01/08/2016 1&lt;BR /&gt;1 01/10/2016 .&lt;BR /&gt;1 01/11/2016 1&lt;BR /&gt;2 01/13/2016 1&lt;BR /&gt;2 04/04/2016 1&lt;BR /&gt;2 06/05/2016 1&lt;BR /&gt;3 12/16/2016 .&lt;BR /&gt;3 11/17/2016 .&lt;BR /&gt;4 05/18/2016 1&lt;BR /&gt;4 05/18/2016 1&lt;BR /&gt;4 05/18/2016 1&lt;BR /&gt;4 03/16/2016 .&lt;BR /&gt;5 04/10/2016 .&lt;BR /&gt;5 04/16/2016 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;BR /&gt;by id date disease;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output I need:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;disease&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried data step with first.id but it doesn't work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any advice would be greatly appreciated!&amp;nbsp; Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 19:06:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/710901#M218915</guid>
      <dc:creator>lulu3</dc:creator>
      <dc:date>2021-01-12T19:06:08Z</dc:date>
    </item>
    <item>
      <title>Re: Count obervations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/710903#M218916</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table temp as
  select distinct id, date,
     max(disease) as disease
	 from have(where=(disease ne .))
	 group by id,date;

  create table want
  as select distinct id,
     sum(disease) as disease
  from temp group by id;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Jan 2021 19:19:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/710903#M218916</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-12T19:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: Count obervations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/710904#M218917</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input id $ date mmddyy10. disease @@;
format date mmddyy10.;
datalines;
1 01/08/2016 1
1 01/08/2016 1
1 01/10/2016 .
1 01/11/2016 1
2 01/13/2016 1
2 04/04/2016 1
2 06/05/2016 1
3 12/16/2016 .
3 11/17/2016 .
4 05/18/2016 1
4 05/18/2016 1
4 05/18/2016 1
4 03/16/2016 .
5 04/10/2016 .
5 04/16/2016 1
;
run;

data want;
 set have;
 by id date notsorted;
 if first.id then count=0;
 if first.date then count+disease;
 if last.id and count;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Jan 2021 19:12:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/710904#M218917</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-12T19:12:15Z</dc:date>
    </item>
    <item>
      <title>Re: Count obervations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/710921#M218930</link>
      <description>&lt;P&gt;If the data are not sorted, then a recursive PROC FREQ might be the most efficient:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ date mmddyy10. disease @@;
format date mmddyy10.;
datalines;
1 01/08/2016 1
1 01/08/2016 1
1 01/10/2016 .
1 01/11/2016 1
2 01/13/2016 1
2 04/04/2016 1
2 06/05/2016 1
3 12/16/2016 .
3 11/17/2016 .
4 05/18/2016 1
4 05/18/2016 1
4 05/18/2016 1
4 03/16/2016 .
5 04/10/2016 .
5 04/16/2016 1
run;

proc freq data=have noprint;
  table id*date / out=need;
  where disease=1;
run;
proc freq data=need noprint;
  table id / out=want (keep=id count);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first proc freq generates dataset NEED with one observation per ID*DATE, with variables ID DATE COUNT and PERCENT.&amp;nbsp; The WHERE statement forces it to ignore non-disease observations.&amp;nbsp; The only variable in NEED that you care about is ID.&amp;nbsp; Just do a proc freq of ID from NEED to generate the number of unique disease dates for each id.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 05:35:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/710921#M218930</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-13T05:35:13Z</dc:date>
    </item>
    <item>
      <title>Re: Count obervations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/711003#M218955</link>
      <description>&lt;P&gt;Just to add another solution&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have(where=(not missing(disease))) out=sorted nodupkey;
   by Id date;
run;

proc summary data=sorted nway;
   by Id;
   output out=want1(drop= _type_ rename=(_freq_=disease));
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2021 06:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-obervations-by-id/m-p/711003#M218955</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-01-13T06:52:57Z</dc:date>
    </item>
  </channel>
</rss>

