BookmarkSubscribeRSS Feed
ChloeNguyen
Calcite | Level 5

I want to find how many observations are by customer type in different place. This is the dataset:

Place     Customer's name          Customer type

IL             Alex                                Gold

FL            Mike                               Gold

IL            Mie                                  Gold

IL            Prince                              Silver

FL           Troy                                 Silver

 

For example, I want to count how many observations are by "Gold" customer type in IL and FL?

4 REPLIES 4
CurtisMackWSIPP
Lapis Lazuli | Level 10

Something like this?

 

 

data have;
  infile datalines dsd;
  input Place $ Customer $ Type $;
datalines;
IL,Alex,Gold
FL,Mike,Gold
IL,Mie,Gold
IL,Prince,Silver
FL,Troy,Silver
run;

proc sql;
  create table want as
  select Place,Type,count(*) as count
  from have
  group by Place,Type;
quit;

I

 

CurtisMackWSIPP
Lapis Lazuli | Level 10

If you want them as column variables use this.

 

proc transpose data=want out=want_t(drop = _name_);
  by Place;
  var Count;
  id type;
run;
jimbarbour
Meteorite | Level 14

The Frequency procedure is a good way to get counts.

 

For example:

jimbarbour_0-1600915704337.png

 

Which was produced by the following code:

ODS	NOPROCTITLE;

DATA	Cust_Data;
	INPUT	Place    		:	$CHAR2. 
			Customer_name	:	$CHAR12.
			Customer_type	:	$CHAR12.
			;
datalines;
IL             Alex                                Gold
FL            Mike                               Gold
IL            Mie                                  Gold
IL            Prince                              Silver
FL           Troy                                 Silver
;;;;
RUN;

PROC	FREQ	DATA=Cust_Data;
	TABLES	PLACE*Customer_Type	/	NOROW	NOCUM	NOPERCENT	NOCOL;
RUN;

Jim

andreas_lds
Jade | Level 19

Even proc summary can do this:

proc summary data=have nway;
  class Place CustomerType;
  output out=want(drop=_type_ rename=(_freq_=count));
run;

or proc tabulate:

proc tabulate data=have;
  class CustomerType Place;
  table Place, CustomerType*n;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 393 views
  • 0 likes
  • 4 in conversation