sqlsasenterprise-guide

Changing FROM statement with a variable


I am trying to change the name of the table I am getting my data from

Like this: COREPOUT.KUNDE_REA_UDL_202112 --> COREPOUT.KUNDE_REA_UDL_202203

I create my variable like this:

PROC SQL NOPRINT;
SELECT DISTINCT 
          PERIOKVT_PREV_BANKSL_I_YYMMN6

INTO      :PERIOKVT_PREV_BANKSL_I_YYMMN6

FROM Datostamp_PREV_Kvartal;

This is the code I want to use the variable for.

%_eg_conditional_dropds(WORK.QUERY_FOR_KUNDE_REA_UDL_20_0000);

PROC SQL;
   CREATE TABLE WORK.QUERY_FOR_KUNDE_REA_UDL_20_0000 AS 
   SELECT t1.Z_ORDINATE, 
            (input(t1.cpr_se,w.)) AS KundeNum
      FROM COREPOUT.KUNDE_REA_UDL_202203 t1;
QUIT;

I have tried things like:

FROM string("COREPOUT.KUNDE_REA_UDL_",PERIOKVT_PREV_BANKSL_I_YYMMN6," t1";

I hope you can point me in the right direction.


Solution

  • The solution I ended up with was inspried by @Stu Sztukowski response:

    I made a data step to concat the variable and created a macro variable.

    data Concat_var; 
        str_PERIOKVT_PREV_YYMMN6 = CAT("COREPOUT.KUNDE_REA_UDL_",&PERIOKVT_PREV_BANKSL_I_YYMMN6," t1"); 
    run;
    
    PROC SQL NOPRINT;
    
    SELECT DISTINCT 
              str_PERIOKVT_PREV_YYMMN6
    INTO      :str_PERIOKVT_PREV_YYMMN6
    
    FROM Concat_var;
    

    Then I used the variable in the FROM statement:

    %_eg_conditional_dropds(WORK.QUERY_FOR_KUNDE_REA_UDL_20_0000);
    
    PROC SQL;
       CREATE TABLE WORK.QUERY_FOR_KUNDE_REA_UDL_20_0000 AS 
       SELECT t1.Z_ORDINATE, 
                (input(t1.cpr_se,w.)) AS KundeNum
          FROM &str_PERIOKVT_PREV_YYMMN6;
    QUIT;
    

    I hope this helps someone else in the future.