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

I have the following table:

 

IDcolorVAR_1VAR_2VAR_3 VAR_4
111010
210011
311010
410011
511000
601011
701010
801010
901110
1001111

 

and I want this in the following form:

 

 color
 01
VAR_1 (=1)53
VAR_2 (=1)20
VAR_3 (=1)54
VAR_4 (=1)2

2

 

The following produces separate tables:

data have;
input ID color var_1 var_2 var_3 var_4;
datalines;
1 1 1 0 1 0
2 1 0 0 1 1
3 1 1 0 1 0
4 1 0 0 1 1
5 1 1 0 0 0
6 0 1 0 1 1
7 0 1 0 1 0
8 0 1 0 1 0
9 0 1 1 1 0
10 0 1 1 1 1
;
run;

 

proc freq data=have;

table (var_1 var_2 var_3 var_4)*color/ nopercent norow  nocol;

run;

 

I am looking for a procedure that gives the answer in the same table.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

My 2 cents

 

data have;
input ID color VAR_1 VAR_2 VAR_3 VAR_4;
datalines;
1 1 1 0 1 0
2 1 0 0 1 1
3 1 1 0 1 0
4 1 0 0 1 1
5 1 1 0 0 0
6 0 1 0 1 1
7 0 1 0 1 0
8 0 1 0 1 0
9 0 1 1 1 0
10 0 1 1 1 1
;

proc tabulate data = have;
   class color;
   var var_:;
   table (var_:)*sum=''*f=8., color / row=float;
run;

View solution in original post

8 REPLIES 8
SASJedi
Ammonite | Level 13
Moved to the SAS Procedures community for better visibility.
Check out my Jedi SAS Tricks for SAS Users
PeterClemmensen
Tourmaline | Level 20

My 2 cents

 

data have;
input ID color VAR_1 VAR_2 VAR_3 VAR_4;
datalines;
1 1 1 0 1 0
2 1 0 0 1 1
3 1 1 0 1 0
4 1 0 0 1 1
5 1 1 0 0 0
6 0 1 0 1 1
7 0 1 0 1 0
8 0 1 0 1 0
9 0 1 1 1 0
10 0 1 1 1 1
;

proc tabulate data = have;
   class color;
   var var_:;
   table (var_:)*sum=''*f=8., color / row=float;
run;
SteveDenham
Jade | Level 19

There is a way to do this using PROC MEANS, but it is extremely messy (I just now gave writing the code), and another using PROC SQL, which isn't nearly as bad, but the best way that ends up with the counts in a table automatically is the one proposed by @PeterClemmensen .

 

SteveDenham

dac_js
Quartz | Level 8

Thanks for your help. Here is a follow up question.

Is it possible to get the data in the following format:

 

 color  
 00 (%)11 (%)Total
VAR_2562.5337.58
VAR_32100002
VAR_4 555.56444.449
VAR_52502504

 

Adding an ALL to your code gives me the total column. 

proc tabulate data = have;
class color;
var var_:;
table (var_:)*sum=''*f=8., color all/ row=float;
run;

Could not find a way to add percentage to each cells. 

Ksharp
Super User
data have;
input ID color VAR_1 VAR_2 VAR_3 VAR_4;
datalines;
1 1 1 0 1 0
2 1 0 0 1 1
3 1 1 0 1 0
4 1 0 0 1 1
5 1 1 0 0 0
6 0 1 0 1 1
7 0 1 0 1 0
8 0 1 0 1 0
9 0 1 1 1 0
10 0 1 1 1 1
;

proc tabulate data = have;
   class color;
   var var_:;
   table (var_:) , color*(sum='n'*f=8.0 rowpctsum='n(%)') sum='Total'*f=8.0/ row=float;
run;
dac_js
Quartz | Level 8
It worked perfectly.
Thank you

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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 4153 views
  • 13 likes
  • 5 in conversation