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

Hi all,

I have the following table structure:

 

customerpl1pl2pl3
1aei
2bfj
3cgk
4dhl
5aei
6bfj
7cgk
8dhl
9aei
10bfj
11cgk
12dhl

 

I need to produce the following table:

 

varvalueno of customers
pl1a3
pl1b3
pl1c3
pl1d3
pl2e3
pl2f3
pl2g3
pl2h3
pl3i3
pl3j3
pl3k3
pl3l3

 

Can this be done in one step?

I use sas 9.4.

 

Thanks in advance,

Johnson

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Yes it can be done in one step via PROC FREQ, but you get cleaner results with two steps. 

See the temp table which has the data. 
The want data is the data in a clean, presentable format more in line with what you want. Another option if you're going straight to a report and a single step would be PROC TABULATE. I prefer this though. 

*Run frequency for tables;
ods table onewayfreqs=temp;
proc freq data=sashelp.class;
	table sex age;
run;

*Format output;
data want;
length variable $32. variable_value $50.;
set temp;
Variable=scan(table, 2);

Variable_Value=strip(trim(vvaluex(variable)));

keep variable variable_value frequency percent cum:;
label variable='Variable' 
	variable_value='Variable Value';
run;

*Display;
proc print data=want(obs=20) label;
run;

Here's another option that works if you're reporting but the layout is slightly different.

proc tabulate data=sashelp.class;
class age sex;
table age sex, N;
run;

@Johnson_M wrote:

Hi all,

I have the following table structure:

 

customer pl1 pl2 pl3
1 a e i
2 b f j
3 c g k
4 d h l
5 a e i
6 b f j
7 c g k
8 d h l
9 a e i
10 b f j
11 c g k
12 d h l

 

I need to produce the following table:

 

var value no of customers
pl1 a 3
pl1 b 3
pl1 c 3
pl1 d 3
pl2 e 3
pl2 f 3
pl2 g 3
pl2 h 3
pl3 i 3
pl3 j 3
pl3 k 3
pl3 l 3

 

Can this be done in one step?

I use sas 9.4.

 

Thanks in advance,

Johnson


 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

If you want a table (you didn't really say)

 

proc freq data=have;
   table pl1 pl2 pl3/list;
run;

 

 

--
Paige Miller
Reeza
Super User

Yes it can be done in one step via PROC FREQ, but you get cleaner results with two steps. 

See the temp table which has the data. 
The want data is the data in a clean, presentable format more in line with what you want. Another option if you're going straight to a report and a single step would be PROC TABULATE. I prefer this though. 

*Run frequency for tables;
ods table onewayfreqs=temp;
proc freq data=sashelp.class;
	table sex age;
run;

*Format output;
data want;
length variable $32. variable_value $50.;
set temp;
Variable=scan(table, 2);

Variable_Value=strip(trim(vvaluex(variable)));

keep variable variable_value frequency percent cum:;
label variable='Variable' 
	variable_value='Variable Value';
run;

*Display;
proc print data=want(obs=20) label;
run;

Here's another option that works if you're reporting but the layout is slightly different.

proc tabulate data=sashelp.class;
class age sex;
table age sex, N;
run;

@Johnson_M wrote:

Hi all,

I have the following table structure:

 

customer pl1 pl2 pl3
1 a e i
2 b f j
3 c g k
4 d h l
5 a e i
6 b f j
7 c g k
8 d h l
9 a e i
10 b f j
11 c g k
12 d h l

 

I need to produce the following table:

 

var value no of customers
pl1 a 3
pl1 b 3
pl1 c 3
pl1 d 3
pl2 e 3
pl2 f 3
pl2 g 3
pl2 h 3
pl3 i 3
pl3 j 3
pl3 k 3
pl3 l 3

 

Can this be done in one step?

I use sas 9.4.

 

Thanks in advance,

Johnson


 

novinosrin
Tourmaline | Level 20

@Reeza I was just thinking of tabulate with you and grand master @ballardw  in my mind. 🙂 Kudos!

Johnson_M
Calcite | Level 5

Thank you Reeza,! it worked 🙂

Ksharp
Super User
data have;
infile cards expandtabs truncover;
input customer	(pl1	pl2	pl3) ($);
cards;
1	a	e	i
2	b	f	j
3	c	g	k
4	d	h	l
5	a	e	i
6	b	f	j
7	c	g	k
8	d	h	l
9	a	e	i
10	b	f	j
11	c	g	k
12	d	h	l
;

proc sql;
select distinct 'pl1' as var length=40,pl1 as value,count(distinct customer) as n_customer
 from have group by pl1
union

select distinct 'pl2' as var length=40,pl1,count(distinct customer) as n_customer
 from have group by pl2
union

select distinct 'pl3' as var length=40,pl1,count(distinct customer) as n_customer
 from have group by pl3
;
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
  • 5 replies
  • 1465 views
  • 5 likes
  • 5 in conversation