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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

View solution in original post

8 REPLIES 8
Reeza
Super User

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;

BryanMarks
Calcite | Level 5

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?

Reeza
Super User

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.

BryanMarks
Calcite | Level 5

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

Tom
Super User Tom
Super User

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

BryanMarks
Calcite | Level 5

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

Bryan

Reeza
Super User

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

Tom
Super User Tom
Super User

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?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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