data firstlast;
input string $60.;
First_Word=scan(string,1, 1,);
Last_Word=scan(string, -1,1);
if _n_ = ' ' then last_word =' ' ;
datalines;
Jack and Jill
& Bob & Carol & Ted & Alice &
Leonardo
Gates
! $ % & ( ) * + , - . /
;;;;
proc print data=firstlast;
run;
i want output below like
|
---|
1 | Jack | Jill |
2 | & | & |
3 | Leonardo |
4 | Gates |
5 | ! | / |
Hi,
Thanks for supplying the data in a data step; you're nearly there with your solution. I would advise checking your log for issues in the form of "NOTE:" messages. Also examine the documentation for the scan() function, as some of the arguments you're supplying don't match the scan() function's specification. Lastly _n_ is a numeric type.
Try:
data have;
input string $60.;
datalines4;
Jack and Jill
& Bob & Carol & Ted & Alice &
Leonardo
Gates
! $ % & ( ) * + , - . /
;;;;
data want;
set have;
First_Word = scan(string,1,' ');
Last_Word = scan(substr(string,length(first_word)+1),-1,' ');
run;
proc print data=want;
run;
Thanks & kind regards,
Amir.
Hi,
Thanks for supplying the data in a data step; you're nearly there with your solution. I would advise checking your log for issues in the form of "NOTE:" messages. Also examine the documentation for the scan() function, as some of the arguments you're supplying don't match the scan() function's specification. Lastly _n_ is a numeric type.
Try:
data have;
input string $60.;
datalines4;
Jack and Jill
& Bob & Carol & Ted & Alice &
Leonardo
Gates
! $ % & ( ) * + , - . /
;;;;
data want;
set have;
First_Word = scan(string,1,' ');
Last_Word = scan(substr(string,length(first_word)+1),-1,' ');
run;
proc print data=want;
run;
Thanks & kind regards,
Amir.
I think you can get what you want by counting the number of words in the string, e.g.:
data firstlast;
input string $60.;
First_Word=scan(string,1, ' ');
if countw(string,' ')>1 then
Last_Word=scan(string, -1,' ');
datalines;
Jack and Jill
& Bob & Carol & Ted & Alice &
Leonardo
Gates
! $ % & ( ) * + , - . /
;;;;
run;
_N_ is an automatic variable which is set to the current data step iteration number at the beginning of each iteration. It is numeric and can never be missing (unless it is explicitly set to missing in code). Your use of it makes no sense at all.
What did you think this statement would do:
if _n_ = ' ' then last_word =' ' ;
?
Hello @BrahmanandaRao
I see the issue is with your data. Particularly the second and last lines.
In the second line the ampersands perhaps represent delimiters.
The last line appears to be unnecessary at least for the sample data.
I have modified the code as below. See if this is what you intended.
data firstlast;
input string $60.;
First_Word=scan(string,1,"&");
Last_Word=scan(string, -1,"&");
datalines;
Jack and Jill
Bob &
Carol &
Ted &
Alice &
Leonardo
Gates
;
proc print data=firstlast;
run;
The output will be as follows
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: