Solved
User
Posts: 1

# invalid third argument to function SUBSTR error

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;

Accepted Solutions
Solution
a month ago
Regular Contributor
Posts: 167

## Re: invalid third argument to function SUBSTR error

[ Edited ]

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?

--------------
blog: papersandprograms.com

All Replies
Solution
a month ago
Regular Contributor
Posts: 167

## Re: invalid third argument to function SUBSTR error

[ Edited ]

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?

--------------
blog: papersandprograms.com
Posts: 4,743

## Re: invalid third argument to function SUBSTR error

@PaulBrownPhD 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)

Regular Contributor
Posts: 167

## Re: invalid third argument to function SUBSTR error

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

--------------
blog: papersandprograms.com
Posts: 4,743

## Re: invalid third argument to function SUBSTR error

[ Edited ]

@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.);`

☑ This topic is solved.