BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
cobba
Obsidian | Level 7

Using SAS EG v7.1

I have the following data where an event is represented by an ID and any number of codes (always strings) can be applied to each event.

 

data have;
    input id$ code$ @@;
    datalines;
    aa x1 aa x1 aa x2
    bb x1 bb y1 bb y1 bb z2 bb z4
    cc x1 cc x2 cc y1 cc x1 cc x1 cc q1
    dd z2 dd z2 dd z2 dd q1
    ;
run;

Episode IDs and Codes are always strings but can be of varying lengths. 

 

The data below is what I want. Where the unique codes make up the column headings and the count is performed for each ID.

image.png

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Does this work for you?
Do you need a table or report?

 

proc freq data=have;
table id*code / out=id_counts norow nocol nopercent sparse;
run;

proc transpose data=id_counts out=want (drop = _name_ _label_);
by id;
id code;
var count;
run;

@cobba wrote:

Using SAS EG v7.1

I have the following data where an event is represented by an ID and any number of codes (always strings) can be applied to each event.

 

data have;
    input id$ code$ @@;
    datalines;
    aa x1 aa x1 aa x2
    bb x1 bb y1 bb y1 bb z2 bb z4
    cc x1 cc x2 cc y1 cc x1 cc x1 cc q1
    dd z2 dd z2 dd z2 dd q1
    ;
run;

Episode IDs and Codes are always strings but can be of varying lengths. 

 

The data below is what I want. Where the unique codes make up the column headings and the count is performed for each ID.

image.png


 

View solution in original post

3 REPLIES 3
Reeza
Super User

Does this work for you?
Do you need a table or report?

 

proc freq data=have;
table id*code / out=id_counts norow nocol nopercent sparse;
run;

proc transpose data=id_counts out=want (drop = _name_ _label_);
by id;
id code;
var count;
run;

@cobba wrote:

Using SAS EG v7.1

I have the following data where an event is represented by an ID and any number of codes (always strings) can be applied to each event.

 

data have;
    input id$ code$ @@;
    datalines;
    aa x1 aa x1 aa x2
    bb x1 bb y1 bb y1 bb z2 bb z4
    cc x1 cc x2 cc y1 cc x1 cc x1 cc q1
    dd z2 dd z2 dd z2 dd q1
    ;
run;

Episode IDs and Codes are always strings but can be of varying lengths. 

 

The data below is what I want. Where the unique codes make up the column headings and the count is performed for each ID.

image.png


 

cobba
Obsidian | Level 7

That is what I'm looking for. Thank you. How do I make the report a table?

Reeza
Super User

It already does that, it's in the WANT data set. Change the name if you want it to have a different name. 

 


@cobba wrote:

That is what I'm looking for. Thank you. How do I make the report a table?


 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 362 views
  • 2 likes
  • 2 in conversation