BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10

I have the following data

id      str                date 

1      car               01/01/2004 

1      bus              01/01/2004 

2      car               02/05/2005

2      car               03/06/2006

3      car               02/02/2002

3      truck            02/02/2002 

3     car                03/03/2003

3     truck             03/03/2003 

3     car                04/04/2004

3     bus               04/04/2004 

3     truck             05/05/2005 

 

I want to find the frequency of id who bought items the same day where each id will be counted only once if they bought the same item's combination multiple times. 

Here is my output: 

car-bus    2

car- truck 1 

then I want to find those who bought only one item (each id counted once)

car 1 

truck 1 

Thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

This will require multiple steps, I can't say I am 100% confident in term of understanding what you need, but here is a try, please let us know if it fits your bill:

data have;
	input id      str :$8.               date: mmddyy10.;
	cards;
1      car               01/01/2004 
1      bus              01/01/2004
2      car               02/05/2005
2      car               03/06/2006
3      car               02/02/2002
3      truck            02/02/2002 
3     car                03/03/2003
3     truck             03/03/2003 
3     car                04/04/2004
3     bus               04/04/2004 
3     truck             05/05/2005 
;

/*sort and dedup*/
proc sort data=have out=h_sort nodupkey;
	by id date str;
run;

/*creat group variable*/
data h_grouping;
	do until (last.date);
		set h_sort;
		by id date str;
		length group $ 100;
		group=catx('-',group,str);
	end;
run;

/*count*/
proc sql;
	select group, count(distinct id) as id_ct from
		h_grouping
	group by group
	;
quit;

View solution in original post

2 REPLIES 2
Haikuo
Onyx | Level 15

This will require multiple steps, I can't say I am 100% confident in term of understanding what you need, but here is a try, please let us know if it fits your bill:

data have;
	input id      str :$8.               date: mmddyy10.;
	cards;
1      car               01/01/2004 
1      bus              01/01/2004
2      car               02/05/2005
2      car               03/06/2006
3      car               02/02/2002
3      truck            02/02/2002 
3     car                03/03/2003
3     truck             03/03/2003 
3     car                04/04/2004
3     bus               04/04/2004 
3     truck             05/05/2005 
;

/*sort and dedup*/
proc sort data=have out=h_sort nodupkey;
	by id date str;
run;

/*creat group variable*/
data h_grouping;
	do until (last.date);
		set h_sort;
		by id date str;
		length group $ 100;
		group=catx('-',group,str);
	end;
run;

/*count*/
proc sql;
	select group, count(distinct id) as id_ct from
		h_grouping
	group by group
	;
quit;
lillymaginta
Lapis Lazuli | Level 10
thank you, works!

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
  • 2 replies
  • 710 views
  • 0 likes
  • 2 in conversation