turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- using SUBSTR function extracts last 3 values

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-02-2017 08:16 AM

Hello All,

The below program using substr extracts last 3 values from obs, can anyone please help me to undersatnad the logic **using substr function**

input trt $;

cards;

treat123

treat21

treat1

treat1

trea2

lbdate

;run;

data abc;

set temp;

a=reverse(substr(left(reverse(trt)),1,3));**b=substr(trt,length(trt)-2);**

run;

Thanks

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tinayong

02-02-2017 08:58 AM

Which of these do you already understand?

- What does the SUBSTR function do?
- What are the three parameters for SUBSTR?
- What does the LENGTH function do?
- What does the REVERSE function do?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

02-02-2017 10:06 AM

output of the program

123

t21

at1

at1

ea2

ate

b=substr(trt,length(trt)-2);

length of the variabke is 8 and 8-2 =6,

so for the first observation from the sixth position we get the values using substr function

but from the second observation onwards the obs values decreasing but still substr function extracting last 3 values

So am missing some logic here, may be am not thinking correct way

123

t21

at1

at1

ea2

ate

b=substr(trt,length(trt)-2);

length of the variabke is 8 and 8-2 =6,

so for the first observation from the sixth position we get the values using substr function

but from the second observation onwards the obs values decreasing but still substr function extracting last 3 values

So am missing some logic here, may be am not thinking correct way

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tinayong

02-02-2017 10:20 AM

For that piece of the puzzle ...

The second parameter within SUBSTR is a number that indicates which character to begin with.

When the third parameter is omitted (as is the case here), SUBSTR takes all the remaining characters.

So this code says measure the length of the incoming string. Substract 2 from that. That becomes the starting position. This means that there will be 3 characters available (as long as the length of the original string is at least 3 characters). Better code would be:

if length(trt) >= 3 then b = substr(trt, length(trt)-2);

That protects against calculating a negative starting position, something that SUBSTR would complain about.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tinayong

02-02-2017 10:44 AM

Good time to read the manual methinks, substr is clearly described there.

If you just want the numbers at the end (and they don't appear anywhere else in the string) compress is simpler:

data want; text="abcd123"; num=compress(text," ","kd"); run;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tinayong

02-02-2017 11:50 AM

tinayong wrote:

output of the program

123

t21

at1

at1

ea2

ate

b=substr(trt,length(trt)-2);

length of the variabke is 8 and 8-2 =6,

so for the first observation from the sixth position we get the values using substr function

but from the second observation onwards the obs values decreasing but still substr function extracting last 3 values

So am missing some logic here, may be am not thinking correct way

Perhaps you phrased your question incorrectly. If you are wanting the numbers at the end and not the "last 3 values" then @RW9 has your solution with compress function

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to tinayong

02-02-2017 09:02 AM

Well one method is:

`data want;`

trt="treat123";

want=substr(trt,lengthn(trt)-2);

run;