BookmarkSubscribeRSS Feed
d0816
Quartz | Level 8

I have following dataset "Have". I want to create a table "want"  where the ID is only counted once in a category ClassStartQuarter. If the ClassStartQuarter is missing then the individual did not take a class and therefore do not count in any quarter. Proc freq will give duplicated count, is there other option to get a distinct count for each quarter?

 

Thank you.

 

Dataset: Have
IDClassStartQuarter
1 
1 
1 
1 
1 
22017Q3
22017Q3
22017Q3
22017Q3
32018Q1
32018Q1
32018Q1
32018Q1
32018Q1
32018Q1
32018Q1
32018Q1
42018Q2
42018Q2
42018Q2
42018Q2
42018Q2
42018Q2
52015Q4
52017Q1
52017Q3
52018Q1
52015Q4
52017Q1
52017Q3
52018Q1
52015Q4
52017Q1
52017Q3
52018Q1
52015Q4
52017Q1
52017Q3
52018Q1
52015Q4
52017Q1
52017Q3
52018Q1
52015Q4
52017Q1
52017Q3
52018Q1
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
62017Q2
72017Q2
72017Q3
72018Q3
72017Q2
72017Q3
72018Q3
72017Q2
72017Q3
72018Q3
72017Q2
72017Q3
72018Q3
72017Q2
72017Q3
72018Q3
8 
8 
8 
8 
8 
8 
92016Q1
92017Q1
92017Q4
92016Q1
92017Q1
92017Q4
92016Q1
92017Q1
92017Q4
92016Q1
92017Q1
92017Q4
92016Q1
92017Q1
92017Q4
92016Q1
92017Q1
92017Q4
102017Q4
102017Q4
102018Q2
102018Q2
102017Q4
102017Q4
102018Q2
102018Q2
112017Q4
112017Q4
112017Q4
112017Q4
122014Q3
122017Q1
122014Q3
122017Q1
122014Q3
122017Q1
122014Q3
122017Q1

 

 

Table: Want (This is just an example)
QuarterNo.of Distinct ID
2013Q10
2013Q20
2013Q312
2013Q44
2014Q14
2014Q23
2014Q316
2014Q428
2015Q120
2015Q221
2015Q30
2015Q415
2016Q128
2016Q229
2016Q30
2016Q40
2017Q128
2017Q227
2017Q360
2017Q425
2018Q127
2018Q233
2018Q313
2018Q413
3 REPLIES 3
data_null__
Jade | Level 19

This might work for you.  It counts the Occurrences and the number if ID(subjects) per Quarter.  You may not need the IDGROUP output.

 

data class;
   infile cards missover firstobs=2;
   input id Quarter:yyq.;
   format quarter yyq.;
   cards;
ID	ClassStartQuarter
1	
1	
1	
1	
1	
2	2017Q3
2	2017Q3
2	2017Q3
2	2017Q3
3	2018Q1
3	2018Q1
3	2018Q1
3	2018Q1
3	2018Q1
3	2018Q1
3	2018Q1
3	2018Q1
4	2018Q2
4	2018Q2
4	2018Q2
4	2018Q2
4	2018Q2
4	2018Q2
5	2015Q4
5	2017Q1
5	2017Q3
5	2018Q1
5	2015Q4
5	2017Q1
5	2017Q3
5	2018Q1
6	2017Q3
6	2017Q3
6	2018Q1
6	2018Q1
6	2018Q1
6	2018Q1
6	2018Q1
6	2018Q1
6	2018Q1
6	2018Q1
;;;;
   run;
proc print;
   run;
proc summary nway data=class missing;
   class id quarter;
   output out=u1(drop=_type_ rename=_freq_=occ);
   run;
proc print;
   run;
data quarter;
   do year=2015 to 2018;
      do q=1 to 4;
         Quarter=yyq(year,q);
         output;
         end;
      end;
   keep quarter;
   format quarter yyq.;
   run;

proc summary nway missing data=u1 classdata=quarter;
   class quarter;
   output out=u2(drop=_type_) sum(occ)= idgroup(out[5](id)=);
   run;
proc print label;
   label _freq_='No.of Distinct ID';
   run;


Capture.PNG

 

Astounding
PROC Star

PROC FREQ can do this.  Start by getting the (unsatisfactory) duplicated counts:

 

proc freq data=have;

   where ClassStartQuarter > ' ';

   tables id * ClassStartQuarter / noprint out=unique;

run;

 

These counts contain duplicates, but the output also contains just one observation per ID.  Just count the result:

 

proc freq data=unique;

   tables ClassStartQuarter;

run;

Ksharp
Super User

proc sql;
create table want as
 select Quarter,count(distinct id) as n_distinct_id
  from class
   group by Quarter;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1686 views
  • 0 likes
  • 4 in conversation