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

HI everyone! I've been able to return the first two words of a string and create a third variable with both concatenated.  However, I also want to include special characters that could be included in the words within the string.

 

Here is the code I have so far.  If you notice, the last two lines do not return the special characters within the word and need to do so as such:

 

Appreciate any help on this 😃

 

data input;

input str $60.;

datalines;

 

BLUE

BLUE BLACK

BLUE GREY BROWN

BLUE GREEN RED

BLUE RED YELLOW ORANGE

BLUE$ YELLOW

BLUE/ ORANGE GRAY

;

run;

data test;

set input;

w1 = scan(str, 1);

w2 = scan(str, 2);

length w3 $ 30;

SubStrLength=findc(str, ' ',-length(str));

if SubStrLength=0 then w3=w1;

else w3=trim(catx(' ', w1, w2));

drop SubStrLength;

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

The below will give you the right split with the spl char. I am sure you can do the concat work yourself as you seem smart.

 

data input;

input str $60.;

datalines;
BLUE
BLUE BLACK
BLUE GREY BROWN
BLUE GREEN RED
BLUE RED YELLOW ORANGE
BLUE$ YELLOW
BLUE/ ORANGE GRAY
;


run;

data test;

set input;

w1 = scan(str, 1,' ','m');

w2 = scan(str, 2,' ','m');run;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

The below will give you the right split with the spl char. I am sure you can do the concat work yourself as you seem smart.

 

data input;

input str $60.;

datalines;
BLUE
BLUE BLACK
BLUE GREY BROWN
BLUE GREEN RED
BLUE RED YELLOW ORANGE
BLUE$ YELLOW
BLUE/ ORANGE GRAY
;


run;

data test;

set input;

w1 = scan(str, 1,' ','m');

w2 = scan(str, 2,' ','m');run;
PhatRam33
Fluorite | Level 6
Ahh so close! Many thanks! The 'm', is this what includes the special characters? I read somewhere about an M modifier and not sure if this is what this is.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 5854 views
  • 1 like
  • 2 in conversation