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

Hello, I need to create a new variable based on an existing one that contains a value such as: "SV01 – Study visit out of window"

 

I need to keep everything after the â€“ and remove the leading space so that the new variable contains "Study visit out of window"

 

the lengths of the input variable are not the same.

 

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data test;
have="SV01 – Study visit out of window";
want=substr(have,find(have,"–")+4);
run;

 

data test;
have="SV01 – Study visit out of window";
want=strip(scan(have,-1,"–"));
run;

 

View solution in original post

9 REPLIES 9
tomrvincent
Rhodochrosite | Level 12
1. calc the position of – in the string.
2. substr everything from after that position to the end of the string.
novinosrin
Tourmaline | Level 20
data test;
have="SV01 – Study visit out of window";
want=substr(have,find(have,"–")+4);
run;

 

data test;
have="SV01 – Study visit out of window";
want=strip(scan(have,-1,"–"));
run;

 

mglogan
Obsidian | Level 7

Thank you!

novinosrin
Tourmaline | Level 20

Kindly test thoroughly and come back to us if you jump into some intricacies. Thanks

ballardw
Super User

Because SCAN uses any of the single characters in the third parameter position it may not be the best solution if either the †or " might appear anyplace other than as a pair. Example:

data test;
have="SV01 – Study visit out “of window";
want=strip(scan(have,-1,"–"));
run;

Which returns "of window" as the value for want since the Scan function is using a single delimiting character and reading from right to left (the -1 parameter) to get the end of the string.

 

novinosrin
Tourmaline | Level 20

This should address that problem, perhaps?

 

Convert the â€“ to tab delim and the same approach

 


data test;
have="SV01 – Study visit out “of window";
temp=tranwrd(have,'–','09'x);
want=strip(scan(temp,-1,'09'x));
drop temp;
run;
ballardw
Super User

@novinosrin wrote:

This should address that problem, perhaps?

 

Convert the â€“ to tab delim and the same approach

 


data test;
have="SV01 – Study visit out “of window";
temp=tranwrd(have,'–','09'x);
want=strip(scan(temp,-1,'09'x));
drop temp;
run;

Yes, something like that or other approaches. I mentioned the issue so if the OP had run the code with SCAN and got unexpected results there would be a place to look for the cause. I will not mention how much time I spend running down similar data issues when provided data that some value "always" occurs with another specific value.

 

r_behata
Barite | Level 11
data have;
	var1="SV01 – Study visit out of window"; output;
	var1="– SV01 Study visit out of window"; output;
	var1=" SV01  Study visit out of – window"; output;

run;

data want;
Length var1 var2 $100.;
	set have;
	var2=strip(substr(strip(var1),findw(var1,"–")+3));
run;
kiranv_
Rhodochrosite | Level 12

@novinosrin  way is cleaner and simpler and this is just another way using prxchange. brief explanation of prxchange below

 

^(.+?)? this is first and starting position and indicate that this position could be there or not

(–) -- this is second patter  and has the symbol thing

(.+)$  -- this is third position after second position till the end

/$3/ -- this means replace everything with third position/pattern

 

 

 

data test;
have="SV01 – Study visit out of window";
want=prxchange('s/^(.+?)?(–)(.+)$/$3/', 1, have);
run;

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 9555 views
  • 3 likes
  • 6 in conversation