BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jenim514
Pyrite | Level 9
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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12

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

snoopy369
Barite | Level 11

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.

PGStats
Opal | Level 21

All you need is

length reason $100;
reason = catx(": ", DSDE, scan(DSAE ,3 ,' '));
PG
Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 2324 views
  • 1 like
  • 5 in conversation