DATA Step, Macro, Functions and more

Need Help with Macro Variables Used in File Name

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Need Help with Macro Variables Used in File Name

Hi,

I'm having trouble getting the following code to work.  The SAS code is numbered, with the SYMBOLGEN and MLOGIC used to show that the macro itself appears to be evaluating properly.  However, there is an error with calling the file using the macro variables created in the code, specifically YY6 and QQ6.  Please note that the same error occurs whether the file name (DAT1.POL&YY6&QQ6) is in double quotes or not.

1          OPTIONS SYMBOLGEN MLOGIC;

2          %LET YYC = 14; *CURRENT REPORTING PERIOD YEAR*;

3          %LET QQC = 06; *CURRENT REPORTING PERIOD QUARTER*;

4          %MACRO LIFDATES;

5          %IF &QQC = 06 %THEN %LET QQ6 = 12;

6          %IF &QQC = 12 %THEN %LET QQ6 = 06;

7          %IF &QQC = 06 %THEN %LET YY6 = %EVAL(&YYC-1);

8          %IF &QQC = 12 %THEN %LET YY6 = %EVAL(&YYC);

9          %LET QQP = %EVAL(&QQC);   *PREV QUARTER FOR YEAR-OVER-YEAR COMPS;

10         %LET YYP = %EVAL(&YYC-1); *PREV YEAR FOR YEAR-OVER-YEAR COMPS;

11         %LET QQP6= %EVAL(&QQ6);  *PREV QTR FOR PREV PERIOD (6 MO BEF QQP);

12         %LET YYP6= %EVAL(&YY6-1);*PREV YEAR FOR PREV PERIOD (6 MO BEF QQP);

13         %LET PYE = %EVAL((&YYP*100)+12);

14         %LET PYE12 = %EVAL(((&YYP-1)*100)+12);

15         %MEND LIFDATES;

16         %LIFDATES;

MLOGIC(LIFDATES):  Beginning execution.

SYMBOLGEN:  Macro variable QQC resolves to 06

MLOGIC(LIFDATES):  %IF condition &QQC = 06 is TRUE

MLOGIC(LIFDATES):  %LET (variable name is QQ6)

SYMBOLGEN:  Macro variable QQC resolves to 06

MLOGIC(LIFDATES):  %IF condition &QQC = 12 is FALSE

SYMBOLGEN:  Macro variable QQC resolves to 06

MLOGIC(LIFDATES):  %IF condition &QQC = 06 is TRUE

MLOGIC(LIFDATES):  %LET (variable name is YY6)

SYMBOLGEN:  Macro variable YYC resolves to 14

SYMBOLGEN:  Macro variable QQC resolves to 06

MLOGIC(LIFDATES):  %IF condition &QQC = 12 is FALSE

MLOGIC(LIFDATES):  %LET (variable name is QQP)

SYMBOLGEN:  Macro variable QQC resolves to 06

MLOGIC(LIFDATES):  %LET (variable name is YYP)

SYMBOLGEN:  Macro variable YYC resolves to 14

MLOGIC(LIFDATES):  %LET (variable name is QQP6)

SYMBOLGEN:  Macro variable QQ6 resolves to 12

MLOGIC(LIFDATES):  %LET (variable name is YYP6)

SYMBOLGEN:  Macro variable YY6 resolves to 13

MLOGIC(LIFDATES):  %LET (variable name is PYE)

SYMBOLGEN:  Macro variable YYP resolves to 13

MLOGIC(LIFDATES):  %LET (variable name is PYE12)

SYMBOLGEN:  Macro variable YYP resolves to 13

MLOGIC(LIFDATES):  Ending execution.

17        

18             LIBNAME DAT1 'MRKTCR2.SAS.P4158D1';

24         DATA POLPC; /*PRIOR SIX MONTHS*/ SET DAT1.POL&YY6&QQ6;

                                                         _   _

                                                         22  22

                                                         200 200

WARNING: Apparent symbolic reference YY6 not resolved.

WARNING: Apparent symbolic reference QQ6 not resolved.

ERROR: File WORK.YY6.DATA does not exist.

ERROR: File WORK.QQ6.DATA does not exist.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, -, :, ;, CUROBS, END, INDSNAME, KEY, KEYRESET, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_. 

ERROR 200-322: The symbol is not recognized and will be ignored.


Accepted Solutions
Solution
‎02-11-2015 01:30 PM
Super User
Posts: 19,877

Re: Need Help with Macro Variables Used in File Name

Posted in reply to BryanMarks

If you have a solution that works for you please mark the question answered.

View solution in original post


All Replies
Super User
Posts: 19,877

Re: Need Help with Macro Variables Used in File Name

Posted in reply to BryanMarks

Macro variable scope, Local vs Global

The macro variables exist in the macro but don't exist once the macro has finished executing.

Add the following line to the top of your code to create Global.

%global yy6 qq6;

Occasional Contributor
Posts: 18

Re: Need Help with Macro Variables Used in File Name

Hi, Reeza!  Thanks for replying so quickly!  Does the %GLOBAL command need to be inside or outside the %MACRO LIFDATES; code?  I assume also that all of the other created variables in that section (between %MACRO-%MEND) also need to be referenced in the %GLOBAL command, correct?

Super User
Posts: 19,877

Re: Need Help with Macro Variables Used in File Name

Posted in reply to BryanMarks

Either location. Any macro variable you want to reference outside of the macro needs to be in the global statement.

OR

You can use @Tom recommendation and rewrite the code into a data step using the intnx function, combined with call symputx function that has a third argument to set a macro variable to Global/Local

I'd go with option 2.

Occasional Contributor
Posts: 18

Re: Need Help with Macro Variables Used in File Name

Hi, Tom & Reeza,

I wasn't aware that creating variables within the macro (between %MACRO-%MEND) would limit their existence to only that part of the program.  I'm not familiar with the intnx function at all and have only a small amount of knowledge about the symputx function, so I think I'd better stick to the %GLOBAL code or create the variables outside of the %MACRO-%MEND code. Smiley Happy

All of that other code in the %MACRO LIFDATES will be used to reference files from multiple points in time for comparisons (e.g., year over year, last six months of current year, last six months of previous year).

Thanks for all of your help!


Bryan

Super User
Super User
Posts: 7,079

Re: Need Help with Macro Variables Used in File Name

Posted in reply to BryanMarks

No need for a macro or a data step.

%let date='01JUN2014'd ;

%let pre6m = %sysfunc(intnx(month,&date,-6),YYMMDD4);

%let preyr = %sysfunc(intnx(month,&date,-12),YYMMDD4);

%put &=pre6m &=preyr ;


PRE6M=1312 PREYR=1306

Occasional Contributor
Posts: 18

Re: Need Help with Macro Variables Used in File Name

Hi, Tom!  That suggestion looks promising!  I'll consider rewriting the macro to use it! Smiley Happy

Bryan

Solution
‎02-11-2015 01:30 PM
Super User
Posts: 19,877

Re: Need Help with Macro Variables Used in File Name

Posted in reply to BryanMarks

If you have a solution that works for you please mark the question answered.

Super User
Super User
Posts: 7,079

Re: Need Help with Macro Variables Used in File Name

Posted in reply to BryanMarks

Since your %LET was inside of macro it probably defined a local macro variable that disappeared once the macro ended.

So your reference to YY6 in line 24 is not finding any macro variable with that name.

You could use %GLOBAL to define the macro as global, or just make sure it already exists before calling the macro.  Then the %LET will change the existing macro variable's value instead of making a new local macro variable.

Why not just add

%LET YY6=;

%LET QQ6=;

in between lines 3 and 4.

What is the purpose of all that macro?  Perhaps you could do it easier using INTNX function calls?

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 957 views
  • 9 likes
  • 3 in conversation