BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
everyone
Fluorite | Level 6

I'm trying to create a macro variable containing a date inside of a data step.

 

In the reprex below, I'm trying to create a macro, MyDate, equal to the YearStart2 macro (1-1-2022). 

 

When I run the code below, the MyDate macro returns a value of '     2020', but I want it to return a value of '1-1-2022'. What am I doing wrong? Thank you.

 

/*Create dummy date*/
%let YearStart = %sysfunc(mdy(1,1,2022));
%put &YearStart;

%let YearStart2 = %sysfunc(putn(&YearStart,YYMMDDD10.));
%put &YearStart2;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &YearStartDate2; 								
end;

call symputx('MyDate',quote(MyDate, "'"));
run;

/*&MyDate is returning '     2020', but should be '1-1-2022'*/
%put &MyDate; %put &YearStart2
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
  • variable name mismatch - (yearstart2 versus yearstartdate2)
  • Mixing of types, macro variables resolve as text, so you need to use INPUT to read the macro variable ((yearstart2) in as a SAS date
  • Mixing of types - when applying the quote function, you need to first convert the date to the desired format to have it appear
/*Create dummy date*/
%let YearStart = %sysfunc(mdy(1,1,2022));
%put &YearStart;

%let YearStart2 = %sysfunc(putn(&YearStart,YYMMDDD10.));
%put &YearStart2;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = input("&YearStart2", yymmdd10.); 								
end;

call symputx('MyDate',quote(put(MyDate, mmddyyd10.), "'"));
run;

/*&MyDate is returning '     2020', but should be '1-1-2022'*/
%put &MyDate; %put &YearStart2;

Your code can also be simplified a bit (keeping the same idea:

%let YearStart = %sysfunc(mdy(1,1,2022));
%put &YearStart;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &yearstart; 								
end;

call symputx('MyDate',quote(put(MyDate, mmddyyd10.), "'"));
run;

/*&MyDate is returning '     2020', but should be '1-1-2022'*/
%put &MyDate; 

If you really want to drop the leading zeros I think you need to create a custom format. 

I'll leave that up to you.

 

Spoiler

@everyone wrote:

I'm trying to create a macro variable containing a date inside of a data step.

 

In the reprex below, I'm trying to create a macro, MyDate, equal to the YearStart2 macro (1-1-2022). 

 

When I run the code below, the MyDate macro returns a value of '     2020', but I want it to return a value of '1-1-2022'. What am I doing wrong? Thank you.

 

/*Create dummy date*/
%let YearStart = %sysfunc(mdy(1,1,2022));
%put &YearStart;

%let YearStart2 = %sysfunc(putn(&YearStart,YYMMDDD10.));
%put &YearStart2;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &YearStartDate2; 								
end;

call symputx('MyDate',quote(MyDate, "'"));
run;

/*&MyDate is returning '     2020', but should be '1-1-2022'*/
%put &MyDate; %put &YearStart2

View solution in original post

4 REPLIES 4
Reeza
Super User
  • variable name mismatch - (yearstart2 versus yearstartdate2)
  • Mixing of types, macro variables resolve as text, so you need to use INPUT to read the macro variable ((yearstart2) in as a SAS date
  • Mixing of types - when applying the quote function, you need to first convert the date to the desired format to have it appear
/*Create dummy date*/
%let YearStart = %sysfunc(mdy(1,1,2022));
%put &YearStart;

%let YearStart2 = %sysfunc(putn(&YearStart,YYMMDDD10.));
%put &YearStart2;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = input("&YearStart2", yymmdd10.); 								
end;

call symputx('MyDate',quote(put(MyDate, mmddyyd10.), "'"));
run;

/*&MyDate is returning '     2020', but should be '1-1-2022'*/
%put &MyDate; %put &YearStart2;

Your code can also be simplified a bit (keeping the same idea:

%let YearStart = %sysfunc(mdy(1,1,2022));
%put &YearStart;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &yearstart; 								
end;

call symputx('MyDate',quote(put(MyDate, mmddyyd10.), "'"));
run;

/*&MyDate is returning '     2020', but should be '1-1-2022'*/
%put &MyDate; 

If you really want to drop the leading zeros I think you need to create a custom format. 

I'll leave that up to you.

 

Spoiler

@everyone wrote:

I'm trying to create a macro variable containing a date inside of a data step.

 

In the reprex below, I'm trying to create a macro, MyDate, equal to the YearStart2 macro (1-1-2022). 

 

When I run the code below, the MyDate macro returns a value of '     2020', but I want it to return a value of '1-1-2022'. What am I doing wrong? Thank you.

 

/*Create dummy date*/
%let YearStart = %sysfunc(mdy(1,1,2022));
%put &YearStart;

%let YearStart2 = %sysfunc(putn(&YearStart,YYMMDDD10.));
%put &YearStart2;

%let refmonth = 2;

/*Set Date -- should be set to 1-1-2022*/
data _null_;
if &refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &YearStartDate2; 								
end;

call symputx('MyDate',quote(MyDate, "'"));
run;

/*&MyDate is returning '     2020', but should be '1-1-2022'*/
%put &MyDate; %put &YearStart2

Quentin
Super User

Hi,

 

There's a mistake in the code you posted, which makes it return an error.  I suspect in your real code, this line:

MyDate = &YearStartDate2; 

is actually

MyDate = &YearStart2; 

Note that YearStart2 has the value: 2022-01-01.

In the DATA step when you do:

MyDate = &YearStart2;

It will resolve to:

MyDate = 2022-01-01;

which the data step compiler will see as subtraction so you get MyDate=2020.

You could fix this by adding quotes around the value to make it character, but then you would also need to convert today() to character.

 

Can you describe the big picture a bit?  If you have a macro variable with the value of a SAS date and you want a new macro variable with the date value formatted as MMDDYYD10, you can just use a PUTN statement to convert it, i.e. 

%let YearStart = %sysfunc(mdy(1,1,2022));
%let MyDate=%str(%')%sysfunc(putn(&YearStart,MMDDYYD10))%str(%') ;
%put &=MyDate ;

Returns:

91   %let YearStart = %sysfunc(mdy(1,1,2022));
92   %let MyDate=%str(%')%sysfunc(putn(&YearStart,MMDDYYD10))%str(%') ;
93   %put &=MyDate ;
MYDATE='01-01-2022'

You can also use an open %IF statement to make it conditional, e.g. :

 

%let refdate=2 ;

%if &refdate=1 %then %do ;
  %let MyDate=%str(%')%sysfunc(putn(%sysfunc(today()),MMDDYYD10))%str(%') ;
%end ;
%else %do ;
  %let MyDate=%str(%')%sysfunc(putn(&YearStart,MMDDYYD10))%str(%') ;
%end ;

%put &=MyDate ;

 

 

 

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

So you set YEARSTART to the first of 2022, which is the number 22,646.

You then generate a version of if using the YYMMDD10. format, which will be 2022-01-01.

You then use that same 2022-01-01 string in SAS code to set the variable MYDATE to the value 2,020.  Which is the date 1965-07-13.  You then convert the number 2,020 into a 12 character string and add quotes around so that you can store '        2020' into the macro variable MYDATE.

 

If you want to convert the string 2022-01-01 to an actual date value you will need to do something to it.  You could use INPUT() function with the right informat.

MyDate = input("&YearStart2",yymmdd10.);

Or you could just use the actual date value you had in YEARSTART instead.

MyDate=&yearstart;

If you want the string to look pretty do the same think you did to make YEARSTART2, apply a FORMAT.  If you want it to look like YEARSTART2 then use the same format.

call symputx('MyDate',quote(put(MyDate,yymmdd10.), "'"));

Note if you use either the MMDDYY or DDMMYY format the result will look more like what you asked for '01-01-2022', but half of your audience will think the first two digits mean the month of the year and the other half will think they mean the day of the month.  Would '01-05-2022' mean the first of May or January the fifth?

 

Sajid01
Meteorite | Level 14

Hello
The corrected code is given below

%let YearStart = %sysfunc(mdy(1,1,2022));
%put &YearStart;

%let YearStart2 = %sysfunc(putn(&YearStart,DDMMYYD10.));
%put &YearStart2;

%let refmonth = 2;
/*Set Date -- should be set to 1-1-2022*/
data _null_;
Format Mydate DDMMYYD10.;
if &refmonth = 1 then do;
	MyDate = today();								
end;

else do;
	MyDate = &YearStart.; 	
	put Mydate=;
end;

call symputx('MyDate',put(Mydate,DDMMYYD10.));
run;

/*&MyDate is returning '     2020', but should be '1-1-2022'*/
%put &=MyDate; %put &=YearStart2.;

The last put shows this

Sajid01_1-1675461468822.png

 

 

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
  • 4 replies
  • 4716 views
  • 1 like
  • 5 in conversation