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

My code is as below:

 %let &_DOMAIN = 'S' ;

DATA _NULL_;

  IF &_DOMAIN = 'S' THEN DO;

    %LET _DTLTRAD  = "CAPP_1Q18_ACAS_Detail_R00_20171101_Primary";

 %LET _DTLTRAD1  = "CAPP_1Q18_ACAS_Detail_R00_20171101_Secondary";  END; 

 

 

ELSE IF &_DOMAIN = 'A' THEN DO;

    IF &_state IN ('CO','CT','DE','MA','ME','MN','NH','OR','UT','VT','VA') THEN DO;

    %LET _DTLTRAD  =  "&ST1._APCD_Y&YR.M&MN._eRCE_STND_DET_PRIME.txt" ;

    %LET _DTLTRAD1  = "&ST1._APCD_Y&YR.M&MN._eRCE_STND_DET.txt" ;

    END;

    ELSE IF &_state IN ('AR','KS','WA') THEN DO;

    %LET _DTLTRAD  =  "&ST1._APCD_Y&YR.Q&QT._eRCE_STND_DET_PRIME.txt" ;

    %LET _DTLTRAD1  = "&ST1._APCD_Y&YR.Q&QT._eRCE_STND_DET.txt" ;

   END;

  END;

  PUT &_DTLTRAD;

  PUT &_DTLTRAD1;

  RUN;

 

Result of this is:

 

AR_APCD_Y17Q01_eRCE_STND_DET_PRIME.txt

AR_APCD_Y17Q01_eRCE_STND_DET.txt

 

Where it should be:

 

CAPP_1Q18_ACAS_Detail_R00_20171101_Primary

CAPP_1Q18_ACAS_Detail_R00_20171101_Secondary

 

Can you help? I cant understand why it is going in second loop.

Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Classical misunderstanding of macro and macro timing. The macro PREprocessor (note the emphasis) does its work before the data step is compiled and executed, so all the %let's are worked through, with the results of the final ones sticking. Since no code is created, the do/end blocks of the data step remain empty and do nothing.

 

If you want a data step to create macro variables, use call symput() or call symputx().

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Classical misunderstanding of macro and macro timing. The macro PREprocessor (note the emphasis) does its work before the data step is compiled and executed, so all the %let's are worked through, with the results of the final ones sticking. Since no code is created, the do/end blocks of the data step remain empty and do nothing.

 

If you want a data step to create macro variables, use call symput() or call symputx().

RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are several issues in your.  First off, and most important is to write code which people can read.  This means not shouting code in upper case, using the code window - its the {i} above post area, finishing statments, and not mixing up macro and datastep code.

First issue, you are mixing macro and datastep.  Macro is nothing more than a text find/replace system which is done before any code is executed.  Therfore you cannot set %let conditionally from datastep as they have already resolved by then.  So when the datastep - where the if construct is - is run, all those %let statements have resolved and are no longer part of the code.  What you perhaps want is call symput:

data _null_;
  select("&_domain.");
    when("S") do;
      call symputx("_dtltrad","capp_1q18_acas_detail_r00_20171101_primary");
      call symputx("_dtltrad1","capp_1q18_acas_detail_r00_20171101_secondary");
      end;
    when("A") do;
       ...
      end;
  end;
run;

%put &_dtltrad.;
%put &_dtltrad1.;

Note how I enclose macro variables in double quotes when I use in the datastep - this is key for them to be resolved.  And it is good practice to always end a macro variable with a . as well.

khushi
Calcite | Level 5

Thanks. Will keep in mind regarding the code for future.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 815 views
  • 1 like
  • 3 in conversation