BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ganeshk
Obsidian | Level 7

Hi,

 

I have data table as shown below:

 

ID Group Sub Group Flag
101 A AB 1
110 A AB 1
103 A IJ 1
108 A KL 1
501 B AB 2
503 B CD 2
507 B EF 1
508 B EF 1
509 B EF 1
515 B KL 2
511 B KL 2

 

I need to count unique ID by Flag as shown below:

 

Flag-1
Group Sub Group count of ID
A AB 2
A IJ 1
A KL 1

 

Flag-2
Group Sub Group count of ID
B AB 1
B CD 1
B EF 3
B KL 2

 

Thanks,

Ganesh K

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

The "distinct id" in Fareeza's proc sql keeps it  to a single PROC.  Otherwise, if you don't use a data step, then you probably need two proc's.  Recursive proc freqs?

 

proc freq data=have noprint ;
  tables flag*group*subgroup*id / out=need;
run;
proc freq data=need noprint;
  table flag*group*subgroup / out=want (drop=percent rename=(count=n_ids));
run;

 

x

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16
data have;
input ID 	Group$ 	Sub_Group$ 	Flag;
cards;
101 	A 	AB 	1
110 	A 	AB 	1
103 	A 	IJ 	1
108 	A 	KL 	1
501 	B 	AB 	2
503 	B 	CD 	2
507 	B 	EF 	1
508 	B 	EF 	1
509 	B 	EF 	1
515 	B 	KL 	2
511 	B 	KL 	2
;

proc sort data=have;
by group sub_group flag;
run;

data flag1 flag2;
set have;
by group sub_group flag;
retain count;
if first.flag then count=1;
else count=count+1;
drop id;
if last.flag;
if flag=1 then output flag1;
else if flag=2 then output flag2;
run;
Thanks,
Jag
Reeza
Super User

When counting unique records, SQL is helpful. 

 

Proc SQL;

create table want as

select flag, group, subgroup, count(distinct ID) as uniqueiDs

from have

group by flag, group, subgroup;

quit. 

 

If you want the strucutre shown shown in your post, use a proc report or proc PRINT on the table from the query above. 

Loko
Barite | Level 11

Hello,

 

data have;
infile datalines;
input ID 	Group $	Sub_Group $	Flag;
datalines;
101 A AB 1
110 A AB 1
103 A IJ 1
108 A KL 1
501 B AB 2
503 B CD 2
507 B EF 1
508 B EF 1
509 B EF 1
515 B KL 2
511 B KL 2
;

proc means noprint nway data=have;
class Group Sub_Group;
var flag;
output out=want(drop=_type_ _freq_) N(Flag)=;
run;
Reeza
Super User

Proc means cannot do a distinct count. If your actual data is like your sample data and you have no duplicate rows/repeat customers per flag/group/subgroup this will be fine. 

mkeintz
PROC Star

The "distinct id" in Fareeza's proc sql keeps it  to a single PROC.  Otherwise, if you don't use a data step, then you probably need two proc's.  Recursive proc freqs?

 

proc freq data=have noprint ;
  tables flag*group*subgroup*id / out=need;
run;
proc freq data=need noprint;
  table flag*group*subgroup / out=want (drop=percent rename=(count=n_ids));
run;

 

x

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2511 views
  • 1 like
  • 5 in conversation