BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10

 

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 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

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.

View solution in original post

5 REPLIES 5
Amir
PROC Star

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.

s_lassen
Meteorite | Level 14

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;
BrahmanandaRao
Lapis Lazuli | Level 10
Thank you
s_Lassen
Kurt_Bremser
Super User

_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 =' ' ;

?

Sajid01
Meteorite | Level 14

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

Sajid01_0-1698846624098.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 672 views
  • 0 likes
  • 5 in conversation