BookmarkSubscribeRSS Feed
iSAS
Quartz | Level 8

I have a column with this type of structure:

Last_Name, First_Name Middle_Name 123456789 Company NameX

 

May I know what function should I use to get the value Company NameX? Thanks

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16

please try below code

 

data have;
input text&$100.;
cards;
Last_Name, First_Name Middle_Name 123456789 Company NameX
;

data want;
set have;
want=prxchange('s/(.*\d+\s)(\w+)/$2/oi',-1,strip(text));

run;
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

alternatively with substr

 

data want;
set have;
want=substr(text,index(text,scan(text,5,'')));

run;
Thanks,
Jag
ghosh
Barite | Level 11
namex=scan(text,-1);

company=scan(text,-2);
ChrisNZ
Tourmaline | Level 20

To get any value after the last digit:

data WANT;
  STR1='Last_Name, First_Name Middle_Name 123456789 Company NameX';
  STR2=prxchange('s/.*\d //',1,strip(STR1));
run;

 

Patrick
Opal | Level 21

The scan() function - SCAN(string, count <, character-list <, modifier>>) - with a negative number for the count parameter allows to search a string from the right to the left. A -1 will return the last term (=the first term from the right).

iSAS
Quartz | Level 8
Thank you for taking your time to help Patrick. Appreciate it!

I used the scan function but i'm having issue with the delimiter. The delimiter of my data is "3 spaces". Exaample below:

data have;
input text&$100.;
cards;
Last_Name, First_Name Middle_Name 123456789 Company NameX;
data want;
set have;
want=scan(text,1,' ');
run;

My initial plan was to use COMPBL function to remove multiple spaces and have the single space as a delimiter. However, my issue on that was Full Name and Company Names may have spaces and cannot be separated. May I know the turnaround for this?

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
  • 7 replies
  • 822 views
  • 0 likes
  • 6 in conversation