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

Hi. 

I run into some trouble. 

I have the data in the following long format. 

 

PID 1 - Drug A

PID 1 - Drug B

PID 1 - Drug A

PID 1 - Drug C

PID 2 - Drug B

PID 2 - Drug C

PID 2 - Drug C

There are duplicates and I would like to transform it into wide format like this.

PID 1 = Drug A Drug B Drug C
PID 2 = Drug B Drug C

I tried to use nodupkeys but if I did that, PID 1 would only have Drug A and it would delete the other 2.

Thanks !

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
PROC SORT is correct, but you need to use both ID and DRUG in the BY statement so it removes duplicates along both. And then follow that with PROC TRANSPOSE.

View solution in original post

5 REPLIES 5
hns
Calcite | Level 5 hns
Calcite | Level 5

PID    Drug
1          A

1          B

1          A

1          C

2          B

2          C

2          C

ChrisNZ
Tourmaline | Level 20

1. What are the variables in the before and after data?
  Provide as a data step please.

2. This change will probably make the data harder to use later on.

Reeza
Super User
PROC SORT is correct, but you need to use both ID and DRUG in the BY statement so it removes duplicates along both. And then follow that with PROC TRANSPOSE.
hns
Calcite | Level 5 hns
Calcite | Level 5

Thanks. It works now.

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