Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

sas if else not working when using parentheses

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

sas if else not working when using parentheses

Why is this code giving error:

data test;

length a: $5;
a='2';
if a = '1' then  a ='a' ;
else (
a= 'b');
run;

ERROR: Undeclared array referenced: else.

I realise removing the parentheses will solve the issue, but i want to keep the parentheses for ease of understanding. Ultimately the else box will have many if else if conditions and the parentheses helps greatly.

 


Accepted Solutions
Solution
‎01-29-2015 03:35 PM
Super User
Posts: 19,775

Re: sas if else not working when using parentheses

Posted in reply to eagles_dare13

SAS interprets that as else() which looks like an array.

You can use indenting to help with code understanding.

Addtionally for recoding you could use either a SELECT statement.

View solution in original post


All Replies
Solution
‎01-29-2015 03:35 PM
Super User
Posts: 19,775

Re: sas if else not working when using parentheses

Posted in reply to eagles_dare13

SAS interprets that as else() which looks like an array.

You can use indenting to help with code understanding.

Addtionally for recoding you could use either a SELECT statement.

Frequent Contributor
Posts: 82

Re: sas if else not working when using parentheses


Thanks. Can you show with example what you mean by recoding with SELECT statement.

Super User
Posts: 19,775

Re: sas if else not working when using parentheses

Posted in reply to eagles_dare13

From the docs:

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

select (a);

  when (1) x=x*10;

  when (2);

  when (3,4,5) x=x*100;

  otherwise;

end;

PS. Personally I actually prefer proc format for recoding - my go to reference paper for that is "Proc Format: Not just another pretty face"

Respected Advisor
Posts: 3,156

Re: sas if else not working when using parentheses

Posted in reply to eagles_dare13

You can't use parentheses here. To format your code for better readability, use comments, indentation etc. For example here if you really want to group the part after 'else', try insert some comments where you tried parentheses, such as /**/.

When you do else (), SAS compiler will take 'else' as an array name, like indicated by the error message.

Super User
Posts: 11,343

Re: sas if else not working when using parentheses

Posted in reply to eagles_dare13

This would have worked to reassign the value for A.

data test;

length a: $5;
a='2';
if a = '1' then  a ='a' ;
else a= 'b';
run;

Also a warning: (a='b') Could be a conditional value in other code locations.

C = (a='b'); will assign a value of 1 (typical value for "true" in SAS) to the variable C when a='b' and 0 when not equal.

Super User
Super User
Posts: 7,039

Re: sas if else not working when using parentheses

Posted in reply to eagles_dare13

You do not use () to group statements in SAS.  Use DO: ... END; blocks for that.

IF A='1' THEN DO;

....

END:

ELSE DO;

....

END;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 564 views
  • 0 likes
  • 5 in conversation