BookmarkSubscribeRSS Feed
carl_miles
Fluorite | Level 6

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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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