BookmarkSubscribeRSS Feed
tinayong
Calcite | Level 5

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 

6 REPLIES 6
Astounding
PROC Star

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?
tinayong
Calcite | Level 5
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



Astounding
PROC Star

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
ballardw
Super User

@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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well one method is:

 

data want;
trt="treat123";
want=substr(trt,lengthn(trt)-2);
run;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 5258 views
  • 0 likes
  • 4 in conversation