How do I convert a SAS date such as "30JUL2009"d into YYYYMMDD format (eg 20090730)?
So for instance:
data _null_;
format test ?????;
test=today();
put test=;
run;
Would give me "test=20090730" in the log...
data _null_;
format test yymmddn8.;
test=today();
put test=;
run;
YYMMDDxw. documentation
%let expectdate1=%sysfunc(putn(%eval(%sysfunc(today())-1),yymmddn8.));
You want to use the format yymmddn8. The 'n' means no separator.
Per http://support.sas.com/kb/24/610.html you can specify B for blank, C for colon, D for dash, N for no separator, P for period, or S for slash.
There is this one that should do the trick too
%let today=%sysfunc(today(),yymmddn8.);
%put &today.;
Everything on the link below
https://communities.sas.com/thread/60111
here's how I did it in macro, but surely there must be a format??!!!
%let today=%sysfunc(compress(%sysfunc(today(),yymmddd10.),'-'));
its weird - the INFORMAT yymmdd8. gives YYYYMMDD result, whereas the FORMAT yymmdd8. gives a YY-MM-DD result!!
You can see all date and time formats in Help tab when you enter 'date' to Index tab and then selecr 'date and time formats'
Related
I am trying to convert a string to a numeric in SAS. Currently, it looks like 05/23/2007. My code so far is
Data Data2;
Set Data1;
Input(Date, mmddyy10w.);
If Date > '07/15/2009'd;
run;
I get an error saying that the format mmddyy10w. cannot be recognized. Does anyone know how to fix this?
Correct informat to read date in the scenario is mmddyy10., date literal should be like '14Jul2009'd. as shown in below example
Data Data2;
date= Input('07/15/2009',mmddyy10.);
format date mmddyy10.;
If Date > '14Jul2009'd;
run;
below is the link, which gives good idea format and informats of dates in SAS.
https://support.sas.com/resources/papers/proceedings15/1334-2015.pdf
I'm having trouble subtracting a date from a Macro Variable.
Currently, I create a macro variable by running:
%LET date = %SYSFUNC(TODAY(),MMDDYY10.);
I feel like I should be able to subtract 1 day from &date by doing the following:
%LET newDate = %SYSFUNC(%INTNX('day',&date,-1),date9.);
However, this produces the error:
ERROR: Function name missing in %SYSFUNC or %QSYSFUNC macro function reference.
I need the output for &newDate to be in date9.
Any help would be appreciated, thanks!
Quick answer:
%LET date = %SYSFUNC(TODAY());
%LET newDate = %SYSFUNC(INTNX(day,&date,-1),date9.);
%put &=newdate;
Explanation:
Firstly, best to remove the formatting from &date to ensure it is interpreted correctly as a date. Your original code resolved (today) inside intnx() to 12/06/2016, which then resolved to 12 divided by 6 divided by 2016 - etc.
Secondly, the inner function to %sysfunc() should be a datastep function - indeed, the whole point of %sysfunc() is to bring these functions into sas. %intnx() isn't a macro function, but if if was, then by definition you wouldn't need to wrap it in %sysfunc().
Finally, the 'day' parameter shouldn't be quoted - everything in sas macro is treated as text by default.
#RawFocus, you are correct that there is no need to format the original date (today's date), and it is easier to deal with that way.
Just for completeness, if someone wanted to apply the MMDDYY10. format, this is how it could be done:
%LET date = %SYSFUNC(TODAY(),mmddyy10.);
%LET newDate = %SYSFUNC(INTNX(day,%SYSFUNC(INPUTN(&date,mmddyy10)),-1),date9.);
%put &=date &=newdate;
I currently have a dataset with dates in the format "FY15 FEB". In attempting to format this variable for use with SAS's times and dates, I've done the following:
data temp;
set pre_temp;
yr = substr(fiscal,3,2);
month = substr(fiscal,6,length(fiscal));
mmmyy = month||yr;
input mmmyy MONYY5.;
datalines;
run;
So, I have the strings representing the year and corresponding month. However, running this code gives me the error "The informat $MONYY was not found or could not be loaded." Doing some background on this error tells me that it has something to do with passing the informat a value with the wrong type; what should I alter in order to get the correct output?
*Edit: I see on the SAS support page for formats that "MONYYw. expects a SAS date value as input;" given this, how do I go from strings to a different date format before this one?
When you see a $, it means character value. In this case, you're feeding SAS a character value and giving it a numeric format. SAS inserts the $ for you, but there is no such format in existence.
I'm going to ignore the datalines statement, because I'm not sure why it's there (though I do notice there is no set statement). You might have an easier time just changing your program to:
data temp;
yr = substr(fiscal,3,2);
month = substr(fiscal,6,length(fiscal));
pre_mmmyy = strip(month)||strip(yr);
mmmyy=input(pre_mmmyy,MONYY5.);
run;
you can also remove the "length(fiscal))" from the substring function. The 3rd argument to the substring function is optional, and will go to the end of the string by default.
I'm trying to convert a SAS string of the form "MO-YR" (e.g. "Jan-04") to a SAS date.
Unfortunately, I don't think there is a date format in SAS that takes that form, so I can't just use an input statement like this date = input(datestring, sasformat).
I've been using this site to find date formats: http://v8doc.sas.com/sashtml/lrcon/zenid-63.htm
Thanks,
Michael
MONYY seems to work for me.
data _null_;
input #1 mydate monyy6.;
put mydate= date9.;
datalines;
Jan-04
Dec-12
;;;;
run;
Puts the correct values to the log.
I am trying to convert SAS Macro variable to timestamp and stumped while conversion. The code which I am using is given below.
%let date = '03/15/2013';
%put %sysfunc(inputn(&date,datetime26.6));
Error which I am getting is
WARNING: Argument 1 to function INPUTN referenced by the %SYSFUNC or
%QSYSFUNC macro function is out of range. NOTE: Mathematical
operations could not be performed during %SYSFUNC function execution.
The result of the operations have been set
to a missing value.
Please let me know if someone knows answers to this.
That is not a DATETIME, that is a DATE format (to INPUT, which depends on the incoming data, not the outgoing). You also need to remove the quotes, SYSFUNC treats quotes as characters, not as string delimiters.
%let date = 03/15/2013;
%put %sysfunc(inputn(&date,MMDDYY10.));
To actually create the datetime, you need to use PUT:
%let date = 03/15/2013;
%put %sysfunc(putn(%sysfunc(dhms(%sysfunc(inputn(&date,MMDDYY10.)),0,0,0)),datetime26.));
However, the better way to do this if you can is to use a date constant...
%let date=15MAR2013;
%put "&date."d;
Joe is mostly correct. If you want a datetime string of midnight 3/15/13, then use
%let date = 03/15/2013;
%put %sysfunc(putn(%sysfunc(dhms(%sysfunc(inputn(&date,MMDDYY10.)),0,0,0)),datetime26.));
Just using PUTN on a date string to "convert" a date to datetime will convert the number of days from epoch (01JAN1960) to the number of seconds from epoch.
My preference for working with dates in macro variables is to store the actual numeric value in the macro variable, and if I need to view/print the formatted value then assign a format to it on the fly:
%let date = %sysfunc(mdy(3,15,2013));
%put %sysfunc(putn(&date,date9.));
That allows you to use it in comparisons like the below (which I find is the most common task):
data xx;
set something;
where datefield = &date;
run;