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

Hello, 

 

I have variable values like below:

 

RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE

RIGHT HAND; LEFT LEG

HEART

 

I would like to create a new variable like below:

HEART

LEFT LEG

LEFT LYMPH NODE

RIGHT EXTERNAL LYMPH

RIGHT HAND

RIGHT LYMPH NODE

 

Which means:

1. separate values between special characters

2. Create a new variable in alphabetical order with those separated values.

 

Can you please help me.

 

Thanks,

Nyalamadugu

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use the SCAN() function to parse the strings at the semi-colons.  Output each value as a separate observation.  Then sort.

data have;
  row+1;
  input string $80.;
cards4;
RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE
RIGHT HAND; LEFT LEG
HEART
;;;;

data want;
  set have;
  do index=1 to countw(string,';');
     word=strip(scan(string,index,';'));
     output;
  end;
run;

proc sort;
  by word;
run;

Results

Obs   row   string                                                    index   word

 1     3    HEART                                                       1     HEART
 2     2    RIGHT HAND; LEFT LEG                                        2     LEFT LEG
 3     1    RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE     3     LEFT LYMPH NODE
 4     1    RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE     2     RIGHT EXTERNAL LYMPH
 5     2    RIGHT HAND; LEFT LEG                                        1     RIGHT HAND
 6     1    RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE     1     RIGHT LYMPH NODE

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Use the SCAN() function to parse the strings at the semi-colons.  Output each value as a separate observation.  Then sort.

data have;
  row+1;
  input string $80.;
cards4;
RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE
RIGHT HAND; LEFT LEG
HEART
;;;;

data want;
  set have;
  do index=1 to countw(string,';');
     word=strip(scan(string,index,';'));
     output;
  end;
run;

proc sort;
  by word;
run;

Results

Obs   row   string                                                    index   word

 1     3    HEART                                                       1     HEART
 2     2    RIGHT HAND; LEFT LEG                                        2     LEFT LEG
 3     1    RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE     3     LEFT LYMPH NODE
 4     1    RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE     2     RIGHT EXTERNAL LYMPH
 5     2    RIGHT HAND; LEFT LEG                                        1     RIGHT HAND
 6     1    RIGHT LYMPH NODE; RIGHT EXTERNAL LYMPH; LEFT LYMPH NODE     1     RIGHT LYMPH NODE
Peter_C
Rhodochrosite | Level 12
Missed a feature of INFILE which would simplify the task.
INFILE statement option DLM=';' ensures the input statement parses to semi colons or end-of-line.
Trailing @@ on INPUT statement preserves the buffer until exhausted.
Data ;
Infile cards dlm= ';' truncover ;
Input objectname :$50. @@ ;
Cards4;
Those data lines
;;;;
Proc sort out= wanted ;
By objectname ;
Run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 3 replies
  • 1218 views
  • 0 likes
  • 3 in conversation