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

Hello,

In my dataset each row cointains information about one individual and their diagnosis code. The information on each individual's different diagnos is stored in one variable that can contain multiple diagnosis ( see 'data have'). I would like to split each diagnos code in to separate variables.  ('data want'). 

 

data have;
input id $ ICD $;
cards;
1 I63 H45 T11
2  C03 D25
3  J11 S54 A10 R45
;
run;

 

Data want;

id $ ICD1 $ ICD2 $ ICD3 $ ICD4 $;

1 I63 H45 T11 .
2  C03 D25 . .
3  J11 S54 A10 R45
;
run;

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
4 REPLIES 4
Kurt_Bremser
Super User

Much better: split it into observations:

data want;
set have (rename=(icd=_icd));
length icd $3;
do i = 1 to countw(_icd);
  icd = scan(_icd,i);
  output;
end;
keep id icd;
run;
novinosrin
Tourmaline | Level 20

Hi @Chris_LK_87  Do all those ICD's consistently have a length of 3 bytes?  

Chris_LK_87
Quartz | Level 8
Yes, they have a length of 3 bytes.
ScottBass
Rhodochrosite | Level 12

1.  Format your SAS code using the "SAS Code" icon.

2.  When I paste your code into SAS, the first data step doesn't give the correct results.  The second data step fails.

 

BZZZZZZZZZZZZZZZZZZZZZZZ!  Try again.

 

P.S.: Use the SCAN function and PROC TRANSPOSE (or an array - TRANSPOSE would be more flexible).  Read the doc for more details.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

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
  • 4 replies
  • 1814 views
  • 2 likes
  • 4 in conversation