BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
PrinceAde
Obsidian | Level 7

Hi everyone, please I need clarification with how Substr functions works

data want;

set have;

if sub = "(112)1245674" then do;
code1= substr(sub, 2, 4);
code2 = substr(sub, 6);
end;

run;

I expect code1 variable to be 112, but it gave me 112);

while code1= substr(sub, 2, 3); gave 112.

any explanation as to why?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Why do you expect to get 3 characters when you tell the function to extract 4 characters?

Note that the second parameter is not a position, it is a length.

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

It always a good idea to have a look at the documentation when in doubt about the parameters of a function. The third argument of substr is the number of chars to extract.

PrinceAde
Obsidian | Level 7

Yes I did,

if sub = "(112)1245674" 

I expect code1= substr(sub, 2, 4); to extract 112.

The first "(" is 1 the from there 2,4 should be 112.

So I'm not why it is generating 112).

Thanks.

 

Kurt_Bremser
Super User

Why do you expect to get 3 characters when you tell the function to extract 4 characters?

Note that the second parameter is not a position, it is a length.

PrinceAde
Obsidian | Level 7

Oh. Now clear. I forgot the third argument is the no character specified in length.

Thanks a lot;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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