BookmarkSubscribeRSS Feed
saidatta
Fluorite | Level 6

I'm a learner of SAS interested in pursuing the career towards it so please help me. As i mentioned above can somebody please explain how to insert delimiter in a string like example asdf@gmail.com  to   asdf-@gmail.com by not breaking it into three variables like a=asdf b=@ c=gmail.com.

this is my question please help me im stuck

4 REPLIES 4
sbxkoenk
SAS Super FREQ

Hello,

Look here:

Inserting a substring into a SAS string
By Leonid Batkhan on SAS Users February 15, 2021

https://blogs.sas.com/content/sgf/2021/02/15/inserting-a-substring-into-a-sas-string/

Have a nice weekend,

Koen

 

Tom
Super User Tom
Super User

For that simple example you can with do a replace:

If you know there are two and only two "words" separated by @ you can use scan() to parse it and catx() to build it back.

data test;
  length have want1-want2 $100;
  have='asdf@gmail.com';
  want1=tranwrd(have,'@','-@');
  want2=catx('-@',scan(have,1,'@'),scan(have,2,'@'));
run;

proc print;
run;
Obs         have              want1              want2

 1     asdf@gmail.com    asdf-@gmail.com    asdf-@gmail.com

ballardw
Super User

One of the things to consider when "inserting" into any string variable is the length of the variable. SAS variables when created will have a defined length. If the current value uses all of the available character positions then "inserting" means that the characters on the end of the value will be truncated because there is no place to store them.

 

So, your problem description should include:

The length of the variable (find this with Proc Contents if you don't know another way)

Determine the current longest value(s) held in the variable.

See if the resulting inserted value will still fit.

If not, you will need to do something with the length prior.

 

Here is one way where the length is long enough to hold the inserted character

data example;
   length string $ 50;
   string="asdf@gmail.com";
   string = transtrn(string,'@','_@');
run;

Caution: the Transtrn function will replace all occurrences of the target string, the '@'.

Also, you will not want to run the same code against the same data set repeatedly as you will keep adding _ characters.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 2719 views
  • 0 likes
  • 5 in conversation