<?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 distinct levels of the variable using last. function? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531760#M145618</link>
    <description>&lt;P&gt;Great solution but less intuitive with the level of my SAS proficiency. what is this approach? hashtag? sorry for being clueless&lt;/P&gt;</description>
    <pubDate>Thu, 31 Jan 2019 19:00:59 GMT</pubDate>
    <dc:creator>Cruise</dc:creator>
    <dc:date>2019-01-31T19:00:59Z</dc:date>
    <item>
      <title>Count the number of distinct levels of the variable using last. function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531734#M145597</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to create the indicator variable N_CNT which indicates the number of distinct levels that seq_date had occurred. Hope image below helps. The last column framed in red is what I need. The code creates a mock data and how I failed.&lt;/P&gt;
&lt;P&gt;I'll appreciate your time and correcting my code for me, if possible?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&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-inline" image-alt="SAS WANTED.png" style="width: 470px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26735i04F8E7482DE18CDD/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS WANTED.png" alt="SAS WANTED.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
INPUT PAT_ID DATE_DIAGNOSIS SER_DATE_DX CODE WGT_DX DATE_DIFF;
CARDS;
1 17007 17006 197 3 1  
1 17007 17006 153 1 1  
1 17007 17006 197 3 1  
1 17007 17013 197 3 6  
1 17007 17013 153 1 6  
1 17007 17028 197 3 21  
1 17007 17028 153 1 21  
1 17007 17028 197 3 21  
1 17007 17031 153 1 24  
1 17007 17032 153 1 25  
1 17007 17045 153 1 38  
2 18216 18216 153 1 0  
2 18216 18216 153 1 0  
2 18216 18216 153 1 0  
2 18216 18216 153 1 0  
2 18216 18217 153 1 1  
2 18216 18217 211 6 1  
2 18216 18217 153 1 1  
2 18216 18217 211 6 1  
2 18216 18217 153 1 1  
2 18216 18219 235 3 3  
2 18216 18219 235 3 3  
2 18216 18219 153 1 3  
2 18216 18220 153 1 4  
2 18216 18223 153 1 7  
2 18216 18224 153 1 8  
2 18216 18225 153 1 9  
2 18216 18226 153 1 10 
2 18216 18241 154 1 25 
2 18216 18242 154 1 26  
;

PROC SORT DATA=HAVE;
BY PAT_ID SER_DATE_DX;
RUN;
DATA HAVE1; SET HAVE;
BY PAT_ID SER_DATE_DX;
IF FIRST.PAT_ID THEN SEQ_DATE=1; ELSE IF FIRST.SER_DATE_DX THEN SEQ_DATE+1;
N_CNT=LAST.SEQ_DATE;
RUN; 
DATA HAVE2; SET HAVE1; 
BY PAT_ID SER_DATE_DX;
IF LAST.PAT_ID AND LAST.SER_DATE_DX THEN N_CNT=LAST.SEQ_DATE; 
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Jan 2019 17:40:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531734#M145597</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2019-01-31T17:40:26Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of distinct levels of the variable using last. function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531737#M145600</link>
      <description>&lt;P&gt;This gives you what you want. Changed a little in your code and replaced the last data step with an SQL Procedure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input pat_id date_diagnosis ser_date_dx code wgt_dx date_diff;
cards;
1 17007 17006 197 3 1  
1 17007 17006 153 1 1  
1 17007 17006 197 3 1  
1 17007 17013 197 3 6  
1 17007 17013 153 1 6  
1 17007 17028 197 3 21  
1 17007 17028 153 1 21  
1 17007 17028 197 3 21  
1 17007 17031 153 1 24  
1 17007 17032 153 1 25  
1 17007 17045 153 1 38  
2 18216 18216 153 1 0  
2 18216 18216 153 1 0  
2 18216 18216 153 1 0  
2 18216 18216 153 1 0  
2 18216 18217 153 1 1  
2 18216 18217 211 6 1  
2 18216 18217 153 1 1  
2 18216 18217 211 6 1  
2 18216 18217 153 1 1  
2 18216 18219 235 3 3  
2 18216 18219 235 3 3  
2 18216 18219 153 1 3  
2 18216 18220 153 1 4  
2 18216 18223 153 1 7  
2 18216 18224 153 1 8  
2 18216 18225 153 1 9  
2 18216 18226 153 1 10 
2 18216 18241 154 1 25 
2 18216 18242 154 1 26  
;

proc sort data=have;
   by pat_id ser_date_dx;
run;

data have1; set have;
   by pat_id ser_date_dx;
   if first.pat_id then seq_date=1; 
   else if first.ser_date_dx then seq_date+1;
run;

proc sql;
   create table want as 
   select *,
          count(distinct seq_date) as N_cnt
   from have1
   group by pat_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Jan 2019 17:48:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531737#M145600</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-01-31T17:48:38Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of distinct levels of the variable using last. function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531739#M145602</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

DATA HAVE;
INPUT PAT_ID DATE_DIAGNOSIS SER_DATE_DX CODE WGT_DX DATE_DIFF;
CARDS;
1 17007 17006 197 3 1  
1 17007 17006 153 1 1  
1 17007 17006 197 3 1  
1 17007 17013 197 3 6  
1 17007 17013 153 1 6  
1 17007 17028 197 3 21  
1 17007 17028 153 1 21  
1 17007 17028 197 3 21  
1 17007 17031 153 1 24  
1 17007 17032 153 1 25  
1 17007 17045 153 1 38  
2 18216 18216 153 1 0  
2 18216 18216 153 1 0  
2 18216 18216 153 1 0  
2 18216 18216 153 1 0  
2 18216 18217 153 1 1  
2 18216 18217 211 6 1  
2 18216 18217 153 1 1  
2 18216 18217 211 6 1  
2 18216 18217 153 1 1  
2 18216 18219 235 3 3  
2 18216 18219 235 3 3  
2 18216 18219 153 1 3  
2 18216 18220 153 1 4  
2 18216 18223 153 1 7  
2 18216 18224 153 1 8  
2 18216 18225 153 1 9  
2 18216 18226 153 1 10 
2 18216 18241 154 1 25 
2 18216 18242 154 1 26  
;

data want;
do until(last.pat_id);
set have;
by pat_id ser_date_dx;
if first.ser_date_dx then seq_date=sum(seq_date,1);
end;
ncount=seq_date;
seq_date=0;
do until(last.pat_id);
set have ;
by  pat_id ser_date_dx;
if first.ser_date_dx then  seq_date=sum(seq_date,1);
output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Jan 2019 17:52:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531739#M145602</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-31T17:52:15Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of distinct levels of the variable using last. function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531760#M145618</link>
      <description>&lt;P&gt;Great solution but less intuitive with the level of my SAS proficiency. what is this approach? hashtag? sorry for being clueless&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 19:00:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531760#M145618</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2019-01-31T19:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of distinct levels of the variable using last. function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531770#M145624</link>
      <description>Gracias! Bayarlalaa!</description>
      <pubDate>Thu, 31 Jan 2019 19:12:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531770#M145624</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2019-01-31T19:12:15Z</dc:date>
    </item>
    <item>
      <title>Re: Count the number of distinct levels of the variable using last. function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531772#M145626</link>
      <description>&lt;P&gt;Basically uses a construct called DOW loop. DOW stands for DO Loop and Whitlock honoring the greatest SAS user Ian Whitlock ever to have used SAS. Ian showed the use of such loops for various scenarios. All you need to do is google "SAS DOW loop"&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 19:13:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-the-number-of-distinct-levels-of-the-variable-using-last/m-p/531772#M145626</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-31T19:13:12Z</dc:date>
    </item>
  </channel>
</rss>

