02-02-2017 08:16 AM
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 $;
02-02-2017 08:58 AM
Which of these do you already understand?
02-02-2017 10:06 AM
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.
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;
02-02-2017 11:50 AM
output of the program
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