BookmarkSubscribeRSS Feed
SaschaD
Obsidian | Level 7

Hello,

 

I need to store the date in a variable which I want use in other case later.

 

Below you will find my code. The output of variblae StartDate_OUT is "StartDate=."

 

If I try to write %LET StartDate_OUT = date(); I get an syntax error.

 

Where is my mistake?

 

data _NULL_;
	format StartDate mmddyy10.;
	StartDate = date();
	%LET StartDate_OUT = StartDate=; 
run;

data _NULL_;
	PUT &StartDate_OUT;
run;

 Thanks,

Sascha

7 REPLIES 7
Shmuel
Garnet | Level 18

Replace line

%LET StartDate_OUT = StartDate=; 

by:

call symput('StartDate_Out', startdate);

and instead:

data _NULL_;
	PUT &StartDate_OUT;
run;

you can check the value of the macro variable by:

%put StartDate_Out = &StartDate_OUT;

Check the log; The number displayed is the sas value of the date, i.e. the days past since 01JAN1060.  In a datastep, use format to display this number in a required format.

 

Norman21
Lapis Lazuli | Level 10
startydate?
Norman.
SAS 9.4 (TS1M6) X64_10PRO WIN 10.0.17763 Workstation

Shmuel
Garnet | Level 18

Sorry for the typo. Post was edited.

andreas_lds
Jade | Level 19

Storing formatted dates in macro-variables is not recommended if you plan to use the variable in comparisons with dataset-variables, because even if the same format is applied to the dataset-variable, the value is still a number.

 

%global StartDate;
%let StartDate = %sysfunc(today());
%put &=StartDate;
s_lassen
Meteorite | Level 14

I assume that what you want is to get today's date into the StartDate_OUT macro variable, in the format MMDDYY10.

 

That is simply accomplished with %SYSFUNC:

%let StartDate_OUT=%sysfunc(today(),mmddyy10.);

 

Kurt_Bremser
Super User

Your code does not result in a syntax ERROR:

 73         data _NULL_;
 74           format StartDate mmddyy10.;
 75           StartDate = date();
 76           %LET StartDate_OUT = StartDate=;
 77         run;
 
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 
 78         
 79         data _NULL_;
 80           PUT &StartDate_OUT;
 81         run;
 
 NOTE: Variable StartDate is uninitialized.
 StartDate=.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.00 seconds
       cpu time            0.00 seconds

But you get a NOTE. Will come to that later.

Your first data step:

data _NULL_;
  format StartDate mmddyy10.;
  StartDate = date();
  %LET StartDate_OUT = StartDate=; 
run;

is equivalent to this:

%LET StartDate_OUT = StartDate=;

data _NULL_;
  format StartDate mmddyy10.;
  StartDate = date();
run;

because the macro processor resolves macro statements when they are encountered in the code, before the data step compiler kicks in, compiles the data step code, and runs it.

So you now have assigned this text to the macro variable:

StartDate=

When the macro processor finds this macro variable in the next data step:

data _NULL_;
  PUT &StartDate_OUT;
run;

it replaces the macro variable with the text stored in it, so you get

data _NULL_;
  PUT StartDate=;
run;

which is valid data step syntax, but references a data step variable (startdate) that does not yet exist, so it is created with a missing value, causing the "uninitialized" NOTE.

 

Keep in mind:

  • the macro processor is a text replacement engine used to make writing code easier
  • macro triggers are resolved immediately
  • macro statements do not have access to data step variables
Kurt_Bremser
Super User

Also see Maxim 28. Values stored in macro variables for later use in code rarely need a format. Formatting is only needed if the macro variable in question is used for human-readable display (e-g. in the text of a TITLE statement). In all other cases, the raw values are easier to use when referencing the macro variable.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 1095 views
  • 1 like
  • 6 in conversation