BookmarkSubscribeRSS Feed
myboys2
Fluorite | Level 6

I have a text file that is not delimited and my name field looks like this:

 

JOHN    DOE

MARYELIZABETH    SMITH

JACKIE    DOE

SOPHIE    DOE

 

The field is $50. as name AND there are four spaces in between the firstname and the lastname

 

How do I parse it to give me FIRST NAME AND LAST NAME

 

Thank you in advance

5 REPLIES 5
novinosrin
Tourmaline | Level 20

did you try scan function?

data output;

set input;

FIRST_NAME=scan(name, 1);

 LAST_NAME=scan(name,2);

run;

 

myboys2
Fluorite | Level 6

thanks I did try what you said and also this and both did not work

first_nm=scan(r_name,1,' '); last_nm=scan(r_name,2,' ');

 

myboys2
Fluorite | Level 6
I ended up doing this - thank you 🙂
format lname $15.;
format fname $15.;
lname=scan(r_name,1,' ');
fname=left(scan(r_name,2,' '))
snoopy369
Barite | Level 11

If both first and last name are guaranteed not to have any spaces, you can just use SCAN.

 

If you specify 4 spaces because you want to treat single spaces as non-delimiters, then you have a few options.  The easiest is to use DLMSTR as '    ', I think.  

 

data want;
length first last $50;
infile datalines dlmstr='    ';
input
first $ last $;
;;;;
datalines;
JOHN    DOE
MARY ELIZABETH    SMITH
JACKIE    DOE
SOPHIE    DOE
;;;;
run;

But if you have other fields and want to read them as fixed width, you could read into NAME, then FIND '   ' and then use SUBSTR to parse it, or a perl regular expression.

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