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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

8 REPLIES 8
kiranv_
Rhodochrosite | Level 12

may be you should have

IF quarter=1 THEN DO;

efac="&earnq1";

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

dystopia
Calcite | Level 5

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.

 

Astounding
PROC Star

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.

Reeza
Super User

Post your full code and log. 

 

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

ballardw
Super User

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.

Astounding
PROC Star

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.

andreas_lds
Jade | Level 19

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 1419 views
  • 0 likes
  • 7 in conversation