Help resolving SAS errors - Statement is not valid

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Help resolving SAS errors - Statement is not valid

I am running some SAS code and getting an error 'Statement is not valid or it is used out of proper order'

 

---

 

array dlacac{9} dlacac1-dlacac9; /* array just used to simplify uprating of dlacac1-9 */

 

IF quarter=1 THEN DO;

efac=&earnq1;

 

END;

  

ELSE IF quarter=2 THEN DO;

efac=&earnq2;

  

END;

  

ELSE IF quarter=3 THEN DO;

efac=&earnq3;

 

END;

 

ELSE IF quarter=4 THEN DO;

efac=&earnq4;

 

END;

 

---

 

I have googled and looked at similar posts in the SAS community and the problem seems to be a missing semi-colon at the end of a statement, particularly an END statement. However I have gone through the code carefully and checked that each line ends with a semi-colon as expected.

 

Does anyone have any other ideas for resolving this error? Thank you.


Accepted Solutions
Solution
‎08-29-2017 06:25 AM
Super User
Posts: 6,629

Re: Help resolving SAS errors - Statement is not valid

This log indicates that your program is not part of a DATA step.  It doesn't show what happened before ... could be a missing DATA statement, could be a commented out DATA statement, could be an extra RUN statement ... but whatever came before, the statements you posted are not part of a DATA step.  If you show what came before, you could get a more specific answer.

View solution in original post


All Replies
PROC Star
Posts: 503

Re: Help resolving SAS errors - Statement is not valid

may be you should have

IF quarter=1 THEN DO;

efac="&earnq1";

Super User
Super User
Posts: 9,414

Re: Help resolving SAS errors - Statement is not valid

Sorry, we can tell nothing from what you have posted.  Follow the guidance below the Post button when you start a new thread - 

Post test data in the form of a datastep (use the {i} above the window to post code).

Post your full code, especially shogin where macro variables and such like are created.

Explain the logic.

Post examples of what you want out (if applicable).

Also posting log of the relevant area would be ideal in this case.

 

Fro what you post, the array is meaningless in this code snippet. &earnq1; <- these could mean anything, no definiton.

 

Also, good idea to avoid coding all in upper case, and please always finish macro variables with the '.':

&earnq1.;

This may not matter in some examplpes, but in others it really does, so good practice to always put them in.

New Contributor
Posts: 2

Re: Help resolving SAS errors - Statement is not valid

Sorry, we can tell nothing from what you have posted. Follow the guidance below the Post button when you start a new thread -

Post test data in the form of a datastep (use the {i} above the window to post code).

Post your full code, especially shogin where macro variables and such like are created.

Explain the logic.

Post examples of what you want out (if applicable).

Also posting log of the relevant area would be ideal in this case.

 

Fro what you post, the array is meaningless in this code snippet. &earnq1; <- these could mean anything, no definiton.

 

Also, good idea to avoid coding all in upper case, and please always finish macro variables with the '.':

&earnq1.;

This may not matter in some examplpes, but in others it really does, so good practice to always put them in.

 

Sorry, I would prefer not to post the full code.

 

The variables earnq1-4 are created in a separate macros file:

 

 

%let     earnq1                        = 0.999               ; /* Uprating factor for earnings - Q1  */
%let     earnq2                        = 1.002               ; /* Uprating factor for earnings - Q2  */
%let     earnq3                        = 0.998               ; /* Uprating factor for earnings - Q3  */
%let     earnq4                        = 0.997               ; /* Uprating factor for earnings - Q4  */

 

The log is

 

 

6156     IF quarter=1 THEN DO;
         --
         180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6157        efac=&earnq1.;
            ----
            180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6158     END;
         ---
         180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6159     ELSE IF quarter=2 THEN DO;
         ----
         180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6160        efac=&earnq2.;
            ----
            180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6161     END;
         ---
         180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6162     ELSE IF quarter=3 THEN DO;
         ----
         180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6163        efac=&earnq3.;
            ----
            180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6164     END;
         ---
         180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6165     ELSE IF quarter=4 THEN DO;
         ----
         180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6166        efac=&earnq4.;
            ----
            180

ERROR 180-322: Statement is not valid or it is used out of proper order.

6167     END;
         ---
         180

ERROR 180-322: Statement is not valid or it is used out of proper order.

 

 

Thank you for all the other replies. I tried

 

 

efac = resolve(cats('&earnq', quarter));

although I still got the same error message.

 

Solution
‎08-29-2017 06:25 AM
Super User
Posts: 6,629

Re: Help resolving SAS errors - Statement is not valid

This log indicates that your program is not part of a DATA step.  It doesn't show what happened before ... could be a missing DATA statement, could be a commented out DATA statement, could be an extra RUN statement ... but whatever came before, the statements you posted are not part of a DATA step.  If you show what came before, you could get a more specific answer.

Super User
Posts: 23,262

Re: Help resolving SAS errors - Statement is not valid

Post your full code and log. 

 

If that is your full code then you definitely have errors...

Super User
Posts: 13,304

Re: Help resolving SAS errors - Statement is not valid

With error messages it is best to copy the code and error from the log and paste that into a code box opened with the forum {i} menu icon.

The main message window will reformat text so that the position of the diagnostics in the error message are displayed incorrectly. That error message usually shows a the code with an _ in the line below indicating exactly where SAS encountered the error. Not always the actual cause of the error as a missing ; or other character in previous lines may be the culprit but it gives us a start.

And if the code is in a macro then set:

options mprint symbolgen;

before running so that more of the actual macro behavior is shown.

Super User
Posts: 6,629

Re: Help resolving SAS errors - Statement is not valid

Are you certain that the four macro variables referenced in the program actually exist?

 

Does the log tell you which statement the error message applies to? 

 

If you are sure that the error message applies to one of the statement that you posted, can you show the code that came before the error?

 

We're just guessing at this point.  From what you have posted, it is entirely possible that the code you have shown is 100% working code and the error lies elsewhere.  Or it may be something entirely silly, like the code you posted is not actually part of a DATA step.  We need a few clues here.

Valued Guide
Posts: 519

Re: Help resolving SAS errors - Statement is not valid

[ Edited ]

Afaik there is only one ELSE statement allowed after IF. But you don't need IF at all:

 

efac = resolve(cats('&earnq', quarter));

 

Edit: missed the IFs ...

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 240 views
  • 0 likes
  • 7 in conversation