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
/*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.
@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
/*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.
@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
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 ;
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?
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.