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