BookmarkSubscribeRSS Feed
madara155
Obsidian | Level 7

Hi

I am trying to get a user input (month and year from user) via "date prompt". This input get assigned to a macro variable called SELECTEDMONTH (this is how it is displayed in the log: %LET SelectedMonth = 01Aug2022;).

Then I am trying to create  few other global macro variable based on this SelectedMonth as follows:

%global ColumnHead dummystartValue1;
%global month_ dummystartValue2;
%global year_ dummystartValue3;

data test;
/*extract the three letters corresponding to Month*/
month_ = substr("&SelectedMonth",3,3);
/*extract the last two digits corresponding to year*/
year_ = substr("&SelectedMonth",8,2);
/*Create a new variable holding the month and year*/
ColumnHead = catx("-",month_,year_);
output;
run;

Following is the output I see:

madara155_0-1656896358661.png

Although I can see the above outputs,  when I print all the macro variables at the end Month_, Year_, ColumnHead macros have no values displayed in the log (but SelectedMonth has the original value).

Having defined them as GLOBAL, I thought their values will get updated and retain those values. But it seems I am missing something.

Can someone please explain me what is going on here?

Thanks

 

 

4 REPLIES 4
Tom
Super User Tom
Super User

Where is the code that you used to assign values to the macro variables?

All I see is the code to generate the dataset named TEST.

Tom
Super User Tom
Super User

If you only need the macro variables then just use macro code to convert the string that is in the SelectedMonth macro variable into the strings you want in the Month_, Year_, ColumnHead  macro variables.

%let month_=%substr(&selectedmonth,3,3);
%let year_=%substr(&selectedmonth,8,2);
%let columnhead=&month_.-&year_. ;

If you aren't running the code inside macro there is no need to force MONTH_ and the othe rmacro variables into the GLOBAL symbol table.  Any macro variable created in open code is by definition going to be in the GLOBAL macro table as it will be the only one that is active if no macro are currently executing.

madara155
Obsidian | Level 7

Thanks Tom.

 

PaigeMiller
Diamond | Level 26
%global ColumnHead dummystartValue1;
%global month_ dummystartValue2;
%global year_ dummystartValue3;

Is incorrect syntax if you are trying to assign values to macro variables. %GLOBAL does not assign values. Instead, I think you want this:

 

%LET SelectedMonth = 01Aug2022;
data _null_;
    sasdate=input("&selectedmonth"d,date9.);
    month_ = month(sasdate);
    year_=year(sasdate);
    ColumnHead = catx("-",month_,year_);
    /* Create macro variables and assign values using CALL SYMPUTX */
   call symputx('dummystartvalue1',columnhead);
run;

 

Now that I have given you some code, let me also add that this is normally a very bad idea to include calendar information in variable names. You will work very hard to do things that are much simpler to do .Is the reason you want a calendar value in a variable name because you want to produce a report where one of the columns is headed by "Aug-22"? If so, forget all this macro use and instead use PROC REPORT, it is much simpler, no macros or macro variables needed, macros and macro variables causing lots of problem for beginners (as you have seen).

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 628 views
  • 0 likes
  • 3 in conversation