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
did you try scan function?
data output;
set input;
FIRST_NAME=scan(name, 1);
LAST_NAME=scan(name,2);
run;
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,' ');
Ok Good 🙂
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.