BookmarkSubscribeRSS Feed
carl_miles
Obsidian | Level 7

Consider the dataset

 

Data Exam;

format exam_date date9.;
informat exam_date date9.;

Input ID $ exam_date Test1 $;
Datalines;
9000001 01JAN2023    A,B
9000001 15FEB2023      D,B
9000001 20MAR2023   E,C

9000003 15FEB2023      D,B
9000003 20MAR2023   F,C

9000004 20MAR2023   B

9000004 20MAR2023   G

 

answer1 condition if patient containing A, B, C then only those should in final dataset

my answer1 should be

9000001 A+B+C

9000003 B+C

9000004   B

 

 

answer2 condition if patient containing other then A, B, C then only those should in final dataset

my answer2 should be

9000001 D+E

9000003 D+F

9000004 G

please write a code that will help to achieve this result

4 REPLIES 4
andreas_lds
Jade | Level 19

There are many ways to create the datasets you need. But i don't think that posting a solution will help you to solve tasks in the future. So, what have you tried?

Amir
PROC Star

Hi,

 

As others have said, sharing what you have tried will help both you and us. I would also advise sharing the log if you have run the code, including any notes, warnings, or error messages.

 

Please use the Insert Code icon "</>" when posting code or log content.

 

 

Thanks & kind regards,

Amir.

Kurt_Bremser
Super User

How I would approach it:

  • in a DATA step, create two datasets (ans1 and ans2), both containing id and new variable test
  • loop through the test1 variable (COUNTW and SCAN functions), and write to ans1 if test is in A,B,C or to ans2 if not
  • sort both dataset by id and test with NODUPKEY to remove duplicates
  • in two final DATA steps, concatenate the tests (use BY, FIRST. and LAST., RETAIN, CATX)

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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