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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 4658 views
  • 6 likes
  • 4 in conversation