IF/THEN in PROC SQL statement of SAS

Posted on 2014-08-08
Last Modified: 2016-02-13
Having problems with an IF/THEN statement in a PROC SQL; statement of SAS.  What am I missing or doing wrong?  Not even getting any errors...just not doing what it should:

proc sql;
      create table NEW_TBL as
            select distinct
                  FIELD1, FIELD2, FIELD3, FIELD4, FIELD5,  
                  %IF &lob. = 'monkey' %THEN
                                                FIELD6, FIELD7
      from OLD_TBL;
Question by:Lee R Liddick Jr
    LVL 23

    Expert Comment

    LVL 11

    Expert Comment


    You are using macro %If's - so I assume you are running this in a Macro.

    The best way to check what the output of a Macro is - is to use:


    Open in new window

    Then set the macro variable to 'monkey', and see what pops out in the log

    Author Comment

    by:Lee R Liddick Jr
    This is in a macro...not sure where to put this mprint.  I'm tried a few things and nothing really is showing in the log.

    Author Comment

    by:Lee R Liddick Jr
    The case statement doesn't work in this.  I've tried a couple times a couple different ways...including the link you provided.
    LVL 14

    Accepted Solution

    does changing  %IF &lob. = 'monkey' %THEN
    to  %IF "&lob" = "monkey" %THEN help?

    Author Comment

    by:Lee R Liddick Jr
    YES...that did it.  Double quotes...really?  Thank you!
    LVL 14

    Expert Comment

    by:Aloysius Low
    actually, because the value passed in was just monkey, thus to do a string comparison, you need to add quotation marks... however, when doing macros, single quotation marks results in the variable being un-parsed i.e. treated as literal value ('&lob' really means &lob instead of monkey), so you'll need to use double quotes to enclose both the variable ("&lob") and the string value ("monkey") :)

    your original code would have worked if you had passed in 'monkey' instead of monkey into the macro

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
    CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now