BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lam1
Calcite | Level 5


Taking over code from colleague who left company.
Not sure I understand what the line 'else if...' is doing as I get the following error.
Thank you.

 

Error" Invalid third argument to function SUBSTR
id2=0012312345 Pat_id=55555 Enc_type=office visit
region=NY id2=12312345 id=12312345 age=30 bmi 20.8 _ERROR_=1 _N_=6


Variable id2 is text length 11:

id2
0012312345
0023454321
0034565656
0099912345

 

Code:
data two;
set one (rename=(id2=id3));
by pat_id;
region='NY';

IF region='MN' then ID2=compress(ID3);

Else if region='NY' then ID2=compress(put(input(substr(id3, 3, 11),11.),11.) );

 

id = input(id2,15.);
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
pau13rown
Lapis Lazuli | Level 10

id3 is 10 chars long and substr 3rd argument is 11. Although length is specified as 11, thus check if it ever exceeds 10 chars, if not change to substr(id3, 3, 10)

 

i have a supplementary Q: does anyone know if you can specify a 3rd argument as missing which would indicate: from the 2nd argument to the end of the string?

View solution in original post

4 REPLIES 4
pau13rown
Lapis Lazuli | Level 10

id3 is 10 chars long and substr 3rd argument is 11. Although length is specified as 11, thus check if it ever exceeds 10 chars, if not change to substr(id3, 3, 10)

 

i have a supplementary Q: does anyone know if you can specify a 3rd argument as missing which would indicate: from the 2nd argument to the end of the string?

Patrick
Opal | Level 21

@pau13rown wrote:

 

i have a supplementary Q: does anyone know if you can specify a 3rd argument as missing which would indicate: from the 2nd argument to the end of the string?


Yes, just don't define a 3rd argument: substr(id3, 3)

pau13rown
Lapis Lazuli | Level 10

great to know, i always tried "substr(id3, 3, )" with a superfluous comma

Patrick
Opal | Level 21

@lam1 wrote:


Taking over code from colleague who left company.
Not sure I understand what the line 'else if...' is doing as I get the following error.
Thank you.

 

Error" Invalid third argument to function SUBSTR
id2=0012312345 Pat_id=55555 Enc_type=office visit
region=NY id2=12312345 id=12312345 age=30 bmi 20.8 _ERROR_=1 _N_=6


Variable id2 is text length 11:

id2
0012312345
0023454321
0034565656
0099912345

 

Code:
data two;
set one (rename=(id2=id3));
by pat_id;
region='NY';

IF region='MN' then ID2=compress(ID3);

Else if region='NY' then ID2=compress(put(input(substr(id3, 3, 11),11.),11.) );

 

id = input(id2,15.);
run;

 


@lam1

That looks to me like a piece of unfinished messy code which you better re-write from scratch

 

 

 

Here region gets set to NY so below IF condition will never be true.

region='NY';
IF region='MN' then ID2=compress(ID3);

That looks like an attempt to convert a string of digits to a number.

Else if region='NY' then ID2=compress(put(input(substr(id3, 3, 11),11.),11.) );
id = input(id2,15.);

There is absolutely no need to first try and strip leading zeros from the string for such a conversion. Code as below should do.

id=input(id3,? best32.);

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 9407 views
  • 7 likes
  • 3 in conversation