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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

6 REPLIES 6
Reeza
Super User

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.

eagles_dare13
Obsidian | Level 7


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

Reeza
Super User

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"

Haikuo
Onyx | Level 15

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.

ballardw
Super User

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.

Tom
Super User Tom
Super User

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

IF A='1' THEN DO;

....

END:

ELSE DO;

....

END;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

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