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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8160 views
  • 7 likes
  • 3 in conversation