BookmarkSubscribeRSS Feed
saitejaguduru97
Calcite | Level 5

Hi All,

I need your help, I would like to know how to extract the position value after the second word (i.e., Kamireddy Jagan Reddy). I want the position number for (i.e.,reddy).

data ds1;
input name:&$35.;
cards;
Kamireddy jagan reddy
Adithya Desh Pandey
Ankit Patel Rathod
;run;

6 REPLIES 6
FreelanceReinh
Jade | Level 19

Hi @saitejaguduru97 and welcome to the SAS Support Communities!

 

You can use the CALL SCAN routine to obtain the position and length of the nth word of a string.

 

Example:

data want;
set ds1;
call scan(name,3,pos,len);
run;

Variable POS will contain the position of the first character of the third word in NAME (using default delimiters as word boundaries) and variable LEN the length of this word. (Both are set to zero if there is no third word.)

 

So if you need the position of the last character of the second word, you can calculate it like this:

data want;
set ds1;
call scan(name,2,pos,len);
word2end=pos+len-1;
run;
saitejaguduru97
Calcite | Level 5

Thank you So much for your reply....

I have one more doubt regarding the same. Please find the below example:-

See the first variable has separated by 4 spaces and after that separated with 3 spaces only in which i need 4th position and 3rd position numbers. Can i get?

data ds1;
input name$35.;
datalines;
kamireddy jagan mohan reddy
nara chandrababu naidu
konidela pawan kalyan
;run;

PaigeMiller
Diamond | Level 26
data want;
set ds1;
num_words=countw(name);
call scan(name,num_words,pos,len);
run;
--
Paige Miller
saitejaguduru97
Calcite | Level 5

Thank you Miller i got the result.

Can i get the 1 and 2 position values with the provided coded?

 

PaigeMiller
Diamond | Level 26

This was already explained by @FreelanceReinh 

--
Paige Miller
FreelanceReinh
Jade | Level 19

@saitejaguduru97 wrote:

Thank you So much for your reply....

(...)

See the first variable has separated by 4 spaces and after that separated with 3 spaces only in which i need 4th position and 3rd position numbers. Can i get?

(...)
datalines;
kamireddy jagan mohan reddy
nara chandrababu naidu


You're welcome.

 

For the last word of a string you can use -1 as the second argument of the CALL SCAN routine (see count argument in the documentation).

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 1084 views
  • 1 like
  • 3 in conversation