DATA Step, Macro, Functions and more

Separating variable from a list

Accepted Solution Solved
Reply
Super Contributor
Posts: 328
Accepted Solution

Separating variable from a list

Dear Sir/Madam,

 

I have data set with variable CodedDCList as below. I want to separate it and create many different variables such that it should look like table 2 below. Please Help me with the code

 

 

 

SAS Output

Obs CodedDxList
1 E78.5 -- Other and unspecified hyperlipidemia; R06.09, R09.89 -- Other dyspnea and respiratory abnormality; I10 -- Unspecified essential hypertension; J44.9 -- Chronic airway obstruction, not elsewhere classified; K21.9 -- Esophageal reflux; M17.10 -- Osteoarthrosis, unspecified whether generalized or localized, lower leg; K44.9 -- Diaphragmatic hernia without mention of obstruction or gangrene
2 I10 -- Unspecified essential hypertension; E04.2 -- Nontoxic multinodular goiter; E78.5 -- Other and unspecified hyperlipidemia
   

 

Table 2
Code1             Code2              Code 3           Code4        Code5       Code6            Code
1)  E78.5          R06.09, R09.89         I10               J44.9        K21.9            M17.10               K44.

            


Accepted Solutions
Solution
‎01-24-2018 03:38 PM
Super User
Posts: 2,048

Re: Separating variable from a list

[ Edited ]
Posted in reply to desireatem

quick and lazy approach:

data have;
infile datalines truncover;
input Obs $ CodedDxList & $800.;
datalines4;
1	E78.5 -- Other and unspecified hyperlipidemia; R06.09, R09.89 -- Other dyspnea and respiratory abnormality; I10 -- Unspecified essential hypertension; J44.9 -- Chronic airway obstruction, not elsewhere classified; K21.9 -- Esophageal reflux; M17.10 -- Osteoarthrosis, unspecified whether generalized or localized, lower leg; K44.9 -- Diaphragmatic hernia without mention of obstruction or gangrene
2	I10 -- Unspecified essential hypertension; E04.2 -- Nontoxic multinodular goiter; E78.5 -- Other and unspecified hyperlipidemia
;;;;

data want;
set have;
array code(100) $20;
k=countw(CodedDxList,'--');
do _n_=1 to k-1;
code(_n_)=scan(scan(CodedDxList,_n_,'--'),-1,';');
end;
drop k;
run;

View solution in original post


All Replies
Solution
‎01-24-2018 03:38 PM
Super User
Posts: 2,048

Re: Separating variable from a list

[ Edited ]
Posted in reply to desireatem

quick and lazy approach:

data have;
infile datalines truncover;
input Obs $ CodedDxList & $800.;
datalines4;
1	E78.5 -- Other and unspecified hyperlipidemia; R06.09, R09.89 -- Other dyspnea and respiratory abnormality; I10 -- Unspecified essential hypertension; J44.9 -- Chronic airway obstruction, not elsewhere classified; K21.9 -- Esophageal reflux; M17.10 -- Osteoarthrosis, unspecified whether generalized or localized, lower leg; K44.9 -- Diaphragmatic hernia without mention of obstruction or gangrene
2	I10 -- Unspecified essential hypertension; E04.2 -- Nontoxic multinodular goiter; E78.5 -- Other and unspecified hyperlipidemia
;;;;

data want;
set have;
array code(100) $20;
k=countw(CodedDxList,'--');
do _n_=1 to k-1;
code(_n_)=scan(scan(CodedDxList,_n_,'--'),-1,';');
end;
drop k;
run;
Super User
Posts: 13,898

Re: Separating variable from a list

Posted in reply to novinosrin

From what the OP shows I think this modification of @novinosrin's code gets a bit closer.

 

data want;
   set have;
   array code(100) $25;/* the length here needs to be large enough the hold the maximum number of codes *8 */
   k=countw(CodedDxList,';');
   do _n_=1 to k;
      code(_n_)=strip(scan(scan(CodedDxList,_n_,';'),1,'--'));
   end;
   drop k;
run;
Super Contributor
Posts: 328

Re: Separating variable from a list

Thanks, you are right

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 124 views
  • 0 likes
  • 3 in conversation