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;
Related
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 want to assign the current year in a YY format to either a macro or data set variable.
I am able to use the automatic macro variables &sysdate or &sysdate9 to get the current date. However, extracting the year in a YY format is proving to be a nightmare. Below are some examples of what I've been trying.
There exists the YEARw. format. But when I try to use it I get errors or weird results. For instance, running
data _null_;
yy = year(input("&sysdate9.", year2.));
put yy=;
run;
produces the error
ERROR 48-59: The informat YEAR was not found or could not be loaded.
If I try to format the variable in the output, I get 1965 instead of the current year. The following
data _null_;
yy = year(input("&sysdate9.", date9.));
put yy= yy year2.;
run;
outputs
yy=2016 65
Please help.
This works to get you the 2-digit year number of the current year:
DATA _NULL_;
YEAR = PUT(TODAY(),YEAR2.);
PUT YEAR;
RUN;
/* Returns: 16 */
To breakdown what I am doing here:
I use TODAY() to get the current date as a DATE type. &SASDATE needs to be converted to a DATE, but also it is the date that the SAS session started. TODAY() is the current date.
PUT allows us to pass in a non-character (numeric/date) value, which is why it is used with TODAY() as opposed to INPUT.
I think it is worth exploring the issues here in more detail.
First, Formats are patterns for converting numeric values to a human readable format. That's what you want to do here: convert a date value to a human readable format, in this case to a year.
Informats, on the other hand, convert human readable information to numeric values. That's not what you're doing here; you have a value already.
Second, put matches with Formats, and input matches with informats, exclusively.
Third, you get close in your last try: but you misuse the year format. Formats are basically value mappings, so they map every possible numeric value in their range (sometimes "all values" is the range, sometimes not) to a display value (string). You need to know what kind of value is expected on the input. YEARw. expects a date value as input, not a year value: meaning input is "number of days from 1/1/1960", mapped to "year". So you cannot take a value you've already mapped to a year value and map it again with that method; it will not make any sense.
Let's look at it:
data _null_;
yy = year(input("&sysdate9.", date9.));
put yy= yy year2.;
run;
yy contains the result of the year function - 2016. Good so far. Now, you need the 2 digit year (16); you can get that through mod function, if you like, or put/substr/input:
data _null_;
yy = input(substr(put(year(input("&sysdate9.", date9.)),4.),3,2),2.);
put yy=;
run;
mod is probably easier though since it's a number. But of course you could've used year:
data _null_;
yy = put(input("&sysdate9.", date9.),year2.);
put yy=;
run;
Now, yy is character, so you could wrap that with input(...,2.) or leave it character depending on your purposes.
Finally - a use note on &sysdate9.. You can easily make this a date without input:
"&sysdate9."d
So:
yy = put("&sysdate9."d,year2.);
That's called a date literal (and "..."dt and "..."t also work for datetime,time). They require things in the standard SAS formats to work properly.
And as pointed out in Nicarus' answer, today() is a bit better than &sysdate9 since it is guaranteed to be today. If you're running this in batch or restart your session daily, this won't matter, but it will if you have a long-running session.
Apply the year function to the date variable
Convert to string
Take last 2 digits
EDIT: change input to PUT
Year = substr(put(year(today()), 4.), 3);
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 quite new to SAS programming and I'm struggling with dates in it.
I have a dataset in SAS where dates are written in this format 16NOV2007:00:00:00 and I need to convert it do this format dd/mm/yyyy
Can anyone help in that?
In the following example
datetime_str is your original datetime, as a string (if it's not a string then all you need is the datepart() function and a proper format).
sasdate is the date part of datetime_str and is stored as a SAS date (which is numeric) but given a ddmmyy format.
date_str (which might not be
needed in your case) is a re-writing of the sasdate into a string
variable, using the same ddmmyy format as before.
SAS Code
data dates;
format datetime_str $20.
sasdate ddmmyys10.
date_str $10.;
datetime_str = "16NOV2007:00:00:00";
sasdate = datepart(input(datetime_str, datetime18.));
date_str = put(sasdate, ddmmyy10.);
run;
Results
datetime_str sasdate date_str
16NOV2007:00:00:00 16/11/2007 16/11/2007
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'