BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PurushReddy
Fluorite | Level 6
I am not understanding below program how it resolved
Data a;
Input string 7.;
Reverse=reverse (string);
pos=indexc(reverse,"., ");
Last_word=substr (string,7-pos+2);
Cards;
ABC.DEF
JHG GHTY
HYT,XYZ
;
RUN;
HERE LAST_WORD STATEMENT HOW IT WILL RESOLVE, PLEASE EXPLAIN
1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

The doc is your friend. Here is a link to the SUBSTR doc.

  • pos = the location of a delimiter in the reverse string. The delimiters are '.' or ',' or ' '.
  • Each string in your data is 7 characters long, so 7-pos gives the position of the character BEFORE the delimiter.
  • Therefore 7-pos+2 gives the position of the character AFTER the delimiter.
  • The SUBSTR function copies all characters from that position until the end of the string.

 

View solution in original post

12 REPLIES 12
LinusH
Tourmaline | Level 20

Insert a put _all_; statement 

Run the program.

Examine log.

Data never sleeps
PurushReddy
Fluorite | Level 6
Thanks for your response,
I want to know how this is working,means flow
Last _word=Substr(string,7-pos+2);
Rick_SAS
SAS Super FREQ

The doc is your friend. Here is a link to the SUBSTR doc.

  • pos = the location of a delimiter in the reverse string. The delimiters are '.' or ',' or ' '.
  • Each string in your data is 7 characters long, so 7-pos gives the position of the character BEFORE the delimiter.
  • Therefore 7-pos+2 gives the position of the character AFTER the delimiter.
  • The SUBSTR function copies all characters from that position until the end of the string.

 

Rick_SAS
SAS Super FREQ

I think you want to declare String to be a CHARACTER variable. Currently, it is a numeric variable. To declare a character variable, put a '$' symbol after the name of the variable on the INPUT statement, like this:

 

Input string $ 7.;

After making that change, rerun the program and examine the output. Let us know if you still have questions!

 

PurushReddy
Fluorite | Level 6
flow of work
Last _word=Substr(string,7-pos+2);
*7-pos+2(here pos value:4)*
maguiremq
SAS Super FREQ

What determines `pos`? Please provide some example data.

PurushReddy
Fluorite | Level 6
Actually this is total dataset, I want to know how(Last_word=substr (string,7-pos+2);).

Data a;
Input string 7.;
Reverse=reverse (string);
pos=indexc(reverse,"., ");
Last_word=substr (string,7-pos+2);
Cards;
ABC.DEF
JHG GHTY
HYT,XYZ
;
RUN;
PaigeMiller
Diamond | Level 26

@PurushReddy wrote:
flow of work
Last _word=Substr(string,7-pos+2);
*7-pos+2(here pos value:4)*

What is your question?

--
Paige Miller
PurushReddy
Fluorite | Level 6
Substr(string,7-pos+2),
How this is working, I am not getting, what is the flow of.
Kurt_Bremser
Super User

Your task can be much easier solved by using the right tool (Maxim 14), which is the SCAN function:

data a;
input string $7.;
last_word = scan(string,-1,"., ");
cards;
ABC.DEF
JHG GHTY
HYT,XYZ
;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 1888 views
  • 5 likes
  • 6 in conversation