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

Hello, I have a dataset that has multiple names separated by '?' in single column. I was able to separate them but some of them have multiple '?' in front of them. Is there a way I can trim only the first '?' from the character name and not the others?

 

X= ?????BLAIN_BRUCE?CONOVER_SCOTTD?BEARDWOOD_JOHNP

 

I only want to remove ? from the beginning and not others. so I want the output to look like

 

BLAIN_BRUCE?CONOVER_SCOTTD?BEARDWOOD_JOHNP

 

x2=compress(x,'?') ;

But this compress removes all ?. I know there is a Btrim function but I would like to use it in data step. Is there anything I can do?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @mjizzle 

 



data want;
X="?????BLAIN_BRUCE?CONOVER_SCOTTD?BEARDWOOD_JOHNP";
Want=substr(x,verify(x,'?'));
run;

If the above works, though simple as it looks, I owe my sincere gratitude to Guru/Master @data_null__  for tons of incredibly overwhelming variety of  solutions in his SAS-L posts. I recommend reading his posts

View solution in original post

6 REPLIES 6
SASKiwi
PROC Star

Try the LEFT function.

mjizzle
Fluorite | Level 6

Tried it, but doesnt it only work for removing spaces?

novinosrin
Tourmaline | Level 20

Hi @mjizzle 

 



data want;
X="?????BLAIN_BRUCE?CONOVER_SCOTTD?BEARDWOOD_JOHNP";
Want=substr(x,verify(x,'?'));
run;

If the above works, though simple as it looks, I owe my sincere gratitude to Guru/Master @data_null__  for tons of incredibly overwhelming variety of  solutions in his SAS-L posts. I recommend reading his posts

mjizzle
Fluorite | Level 6
Perfect!!! It indeed solved my problem. Thank you so much to you and @data_null__
SuryaKiran
Meteorite | Level 14

Alternatively, 

data test;
X= "?????BLAIN_BRUCE?CONOVER_SCOTTD?BEARDWOOD_JOHNP";
do until (scan(x,1,'?','m')^="");
x=substr(x,2);
end;
run;
Thanks,
Suryakiran
novinosrin
Tourmaline | Level 20

Hi @SuryaKiran  I suppose this problem is rather much too simple and straight forward.

 

You could consider

 

Want=substr(x,(notpunct(x)));
Want=substr(x,(anyalnum(x)));

 

and there are many more related approaches so forth. 

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
  • 6 replies
  • 3847 views
  • 6 likes
  • 4 in conversation