Search code examples
sqlmacrossasidentifiermultipart

SAS variable as SQL table name


EDIT: Thank you for the quick responses. I have a lot to learn about SaS obviously but everyone's responses were very helpful.

Beginner here, trying to write SAS code that can be easily ported across systems.

Goal: I want to have PROC SQL use a global variable for a multipart table identifier.

/* global variables*/
%LET lib_name="Merge_Contacts"
%LET table_name="Contacts"


/* Simple Query */
PROC SQL;
    CREATE TABLE Merged_Contacts AS
        SELECT a.*, b.*
        /* Below is the problem area */
        /* SAS doesn't recognize this as a valid lib/table name */
        FROM &lib_name.&table_name 
        ... 
        /*merge another table*/
QUIT;            

I've tried to concatenate this identifier in several ways to no success. Any thoughts? Thanks!


Solution

  • /* global variables*/
    %LET lib_name=Mrg_ctat;  /* lib names 8 characters and you needed ; on
    %LET table_name=Contacts;   both %let statements Also, remove "" */
    
    
    /* Simple Query */
    PROC SQL;
        CREATE TABLE Merged_Contacts AS
            SELECT a.*, b.*
            /* Below is the problem area */
            /* SAS doesn't recognize this as a valid lib/table name */
    
           /* use .. to tag end of first macro variable. This will provide
              single . as separator */
            FROM &lib_name..&table_name 
            ... 
            /*merge another table*/
    QUIT;