BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. 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?


 

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