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
Related
I have a .csv or .txt file which contains a date/timestamp field.
It looks like 'DD/MM/YYYY HH:MM:SS' in the CSV file. When I load this file is from a txt instead of .csv I can see there is trailing fractional seconds that .csv file isn't displaying.
I want the field to be read in as a date/time field 'DD/MM/YYYY HH:MM:SS' (two spaces between date & time) but cant find the correct format to used.
I'm trying to format this field into a usable format and have tried multiple format types, such as DATETIME19., ANYDTM etc, and I have also tried the picture format < https://communities.sas.com/t5/SAS-Data-Management/Timestamp-format-of-YYYY-MM-DD-HH-MM-SS/m-p/314667#M8994>
I currently have the field read in as a character but cant slice the data. Data below is to shown as an example, I'm reading this is from a text file currently.
DATA = '2020-08-24 10:49:17.8653230'
DATA ENTITY;
INFILE ENT
DELIMITER='09'x
DSD
FIRSTOBS=2
TRUNCOVER;
FORMAT CreatedOn $19.;
INPUT CreatedOn $;
RUN;
OUTPUT = '2020-08-24 10:49:17'
I've looked through a lot of online resources and must be missing something as I cant make anything I've tried to work.
I want the fields to be recognized as a date&time field with the DD/MM/YYYY HH:MM:SS format instead of a character field.
I'm running base SAS 9.4.2
Thanks in advance!
Imports fine for me using anydtdtm.
data demo;
example_date = '2020-08-24 10:49:17.8653230';
want = input(example_date, anydtdtm.);
format want datetime22.;
run;
proc print;run;
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
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.
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'