DATA Step, Macro, Functions and more

Error due to parentheses

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Error due to parentheses

Basically, I need to show diagnoses OR rev codes related to mental health. 

 

 

 DATA TARG.DATA;                                             
   SET TARGETF.DATA;                                         
            IF TRIM(DIAG) IN ('F0600' <= DIAG <= 'F6999' OR  
                 'F8400' <= DIAG <= 'F9999' OR               
                 'Z8650' <= DIAG <= 'Z8659') OR              
        LREVCD IN ('0114','0124','0134','0144','0154','0961')
                     THEN MH = 'Y';                          
                     ELSE MH = 'N';                          
     RUN;                                                    


Accepted Solutions
Solution
‎05-04-2017 04:35 PM
Super User
Posts: 17,750

Re: Error due to parentheses

The syntax is incorrect. 

What are you trying to do with the TRIM(DIAG)? There's no condition (AND/OR) between it and the next condition so it's invalid. 

 

I would suggest building each condition individually and then stringing them together if you're unsure of the syntax.

 

View solution in original post


All Replies
Super User
Posts: 17,750

Re: Error due to parentheses

You're combining operators. You can use one at a time, but should also use brackets to ensure the correct order is followed for evaluating the results.

 

if ('F0600' <= DIAG <= 'F6999') or 'F8400' <= DIAG <= 'F9999' or 'Z8650' <= DIAG <= 'Z8659' or  LREVCD IN ('0114','0124','0134','0144','0154','0961') then ...
Super User
Posts: 10,466

Re: Error due to parentheses

Please be aware that character comparisons involving < or > may not work as desired if any values have fewer or more characters.

 

For example 'F85' or 'F9' is in the range 'F8400' to 'F9999' because SAS stops comparing characters when the shortest string is exhausted. So if you might have values like F9 or F85 that you do not want as considered in that range you need to additional restrictions such as "And length(diag) = 5"

Contributor
Posts: 37

Re: Error due to parentheses

50             DATA TARG.DATA;                                                  
51               SET TARGETF.DATA;                                              
52                       IF TRIM(DIAG) ('F0600' <= DIAG <= 'F6999') OR          
                                       _                                        
                                       _                                        
                                       _                                        
                                       22                                       
                                       22                                       
                                       22                                       
ERROR 22-322: SYNTAX ERROR, EXPECTING ONE OF THE FOLLOWING: !, !!, &, *, **, +, 
ERROR 22-322: SYNTAX ERROR, EXPECTING ONE OF THE FOLLOWING: !, !!, &, *, **, +, 
ERROR 22-322: SYNTAX ERROR, EXPECTING ONE OF THE FOLLOWING: !, !!, &, *, **, +, 
              LT, MAX, MIN, NE, NG, NL, OR, ¬=, |, ||, ~=.                      
              LT, MAX, MIN, NE, NG, NL, OR, ¬=, |, ||, ~=.                      
              LT, MAX, MIN, NE, NG, NL, OR, ¬=, |, ||, ~=.                      
                                                                                
53                            'F8400' <= DIAG <= 'F9999' OR                     
54                            'Z8650' <= DIAG <= 'Z8659' OR                     
55                   LREVCD IN ('0114','0124','0134','0144','0154','0961')      
56                                 THEN MH = 'Y';                               
57                                 ELSE MH = 'N';                               
58                 RUN;                                                         
Solution
‎05-04-2017 04:35 PM
Super User
Posts: 17,750

Re: Error due to parentheses

The syntax is incorrect. 

What are you trying to do with the TRIM(DIAG)? There's no condition (AND/OR) between it and the next condition so it's invalid. 

 

I would suggest building each condition individually and then stringing them together if you're unsure of the syntax.

 

Contributor
Posts: 37

Re: Error due to parentheses

Did this instead..

 

DATA TARG.DATA;                                           
  SET TARGETF.DATA;                                       
          IF ('F0600' <= TRIM(DIAG) <= 'F6999' OR         
               'F8400' <= TRIM(DIAG) <= 'F9999' OR        
               'Z8650' <= TRIM(DIAG) <= 'Z8659') OR       
      LREVCD IN ('0114','0124','0134','0144','0154','0961')
                    THEN MH = 'Y';                        
                    ELSE MH = 'N';                        
    RUN;                                                  

Super User
Posts: 17,750

Re: Error due to parentheses

As @ballardw indicated you need to test that thoroughly because it's using character comparisons which may not be correct.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 122 views
  • 1 like
  • 3 in conversation