DATA Step, Macro, Functions and more

pull a word out of a character string and join it with a different char string

Accepted Solution Solved
Reply
Regular Contributor
Posts: 154
Accepted Solution

pull a word out of a character string and join it with a different char string

Spoiler
 

Hi!  I'm trying to pull a word out of a character string and join it with a different char string separated by colon.

 

For variable DSAE, I have values that look like this:

AE: (1) hypertension 16-NOV-2017.  I need to pull out the 'hypertension'

 

Then I need to join with another variable DSDE , with the two values separated by colon.

 

I tried this:

 

if DSAE ne '' then reason = strip(DSDE) || ': ' || scan(DSAE ,3,' ');
else if DSAE = '' then reason = strip(DSDE);

 

But my new variable  REASON is not displaying the new string.

 

Help appreciated!


Accepted Solutions
Highlighted
Solution
2 weeks ago
Super User
Posts: 5,711

Re: pull a word out of a character string and join it with a different char string

Perhaps you have been experimenting with this for a while and might find that REASON is already part of your incoming data set.  In that case, you may need a longer length:

 

data want;

length reason $ 100;

set have;

*** then re-set the value for REASON, using CATX;

run;

 

If REASON is too short, and already defined, it may not be long enough to hold the new string.

View solution in original post


All Replies
PROC Star
Posts: 360

Re: pull a word out of a character string and join it with a different char string

if you can show an example of what you have as data and what you want as data, it will be very easy for someone to help you

Super Contributor
Posts: 311

Re: pull a word out of a character string and join it with a different char string

[ Edited ]

There's no particular reason that shouldn't work as you coded it in theory - see this example:

 

data have;
dsae='AE: (1) hypertension 16-NOV-2017';
dsde='something else';

if DSAE ne '' then reason = strip(DSDE) || ': ' || scan(DSAE ,3,' ');
else if DSAE = '' then reason = strip(DSDE);
run;

Now, you may run into issues with lengths, which could be keeping you from getting your correct result.

 

I also would note that the `else if` there isn't really necessary, it seems like it should just be `else`.  While those two conditions should be the full set of possibilities, you run into issues where you could have a mistake that makes both of them false.  (I'm also not sure what happens if that is processed in SQL for example, where '' and ' ' are different, and NULL is something yet different.)

 

I would code it this way:

 

data have;
  length reason $50;
  dsae='AE: (1) hypertension 16-NOV-2017';
  dsde='something else';

  reason = catx(': ',DSDE,scan(DSAE ,3,' '));
run;

But mostly that's just stylistic differences.  The ELSE is not necessary here as CATX will not add the delimiter if the argument is blank.

 

If the above doesn't work for you, you'll need to give some example data that reproduces your particular issue, and let us know if there's anything complicated here like a non-SAS data source on the SET statement.

 

First try adding a LENGTH statement that's sufficiently long, though, I think that could well be part of your issue.

Respected Advisor
Posts: 5,043

Re: pull a word out of a character string and join it with a different char string

All you need is

length reason $100;
reason = catx(": ", DSDE, scan(DSAE ,3 ,' '));
PG
Highlighted
Solution
2 weeks ago
Super User
Posts: 5,711

Re: pull a word out of a character string and join it with a different char string

Perhaps you have been experimenting with this for a while and might find that REASON is already part of your incoming data set.  In that case, you may need a longer length:

 

data want;

length reason $ 100;

set have;

*** then re-set the value for REASON, using CATX;

run;

 

If REASON is too short, and already defined, it may not be long enough to hold the new string.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 136 views
  • 1 like
  • 5 in conversation