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

So I have two fields, name and type and I am trying to figure how to break this out into more distinct columns.

 

Name has just different names of users

 

Type has different colors.

 

So what I am trying to do is:


Name Count of Red Count of Blue Count of Green
Matt                4                    1                     2  
Ben                 2                    3                    0
Frank              6                    4                    1

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You can do that with proc freq and proc transpose:

data have;
input name $ type $;
cards;
Matt Red
Matt Red
Matt Red
Matt Red
Matt Blue
Matt Green
Matt Green
;
run;

proc freq data=have;
by name;
table type/  out=inter nocum nocol nopercent;
run;

proc transpose data=inter out=want (drop=_name_ _label_) prefix=count_of_;
by name;
id type;
var count;
run;

proc print data=want noobs;
run;

Result:

         count_     count_     count_
name    of_Blue    of_Green    of_Red

Matt       1           2          4  

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Proc freq your starting data, then transpose the output from the proc freq.  Post test data in the form of a datastep if you want actually working code, I am not here to type in test data.

Kurt_Bremser
Super User

You can do that with proc freq and proc transpose:

data have;
input name $ type $;
cards;
Matt Red
Matt Red
Matt Red
Matt Red
Matt Blue
Matt Green
Matt Green
;
run;

proc freq data=have;
by name;
table type/  out=inter nocum nocol nopercent;
run;

proc transpose data=inter out=want (drop=_name_ _label_) prefix=count_of_;
by name;
id type;
var count;
run;

proc print data=want noobs;
run;

Result:

         count_     count_     count_
name    of_Blue    of_Green    of_Red

Matt       1           2          4  
ballardw
Super User

Do you want a data set (used by computer for other processes) or report (people read it);

 

One report

 

Proc tabulate data=have;

    class name color;

   table name,

            color

   ;

run;

IgawaKei29
Quartz | Level 8

I appreciate all the responses on this.  Not used proc transpose a whole lot so this was interesting to see work.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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