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.

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!

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
  • 4 replies
  • 614 views
  • 0 likes
  • 4 in conversation