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?
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.
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.
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.
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.
Oh. Now clear. I forgot the third argument is the no character specified in length.
Thanks a lot;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.