I have a character variable which contains dates like (string length is 30):
Mon Jul 24 02:48:17 -0700 2017
------------------------------ ruler
123456789012345678901234567890
1 2 3
How can I extract the usable(date,month,year) date and save it as numeric date. I know how to do it by using SUBSTR function and then concatenating everything together. But I want to know a easier and quick way. I tried using ANYDTDTE informat to read it, but got blank values. Let me know if there is any simpler way to read this date.
Do you need just the date? Or do you need to get the time part also? The SCAN() function can make this easier.
data test;
str='Mon Jul 24 02:48:17 -0700 2017';
date=input(cats(scan(str,3),scan(str,2),scan(str,-1)),date9.);
format date date9.;
datetime=input(cats(scan(str,3),scan(str,2),scan(str,-1),':',scan(str,4,' ')),datetime20.);
format datetime datetime19.;
run;
Result:
str=Mon Jul 24 02:48:17 -0700 2017
date=24JUL2017
datetime=24JUL2017:02:48:17
Related
I am trying to get file "modeified" datetime with
datetimeString = finfo(fid,'Last Modified');``
In SAS EG the return string looks like 12Jan2023:11:03:53
But in SAS DI the return string looks like 12 January 2023 11:03:28
I am trying to convert a string to a datetime like below and obviously it doesn't work for EG and throw invalid argument error.
moddate=input(finfo(fid,'Last Modified'),datetime20.);
I can fix this by writing bit of extra code in DI but would like to know why finfo(fid,'Last Modified'); return different string format?
I am working in data step.
Check the setting of the LOCALE option in the two different SAS sessions.
You can use the NLDATM informat to read the string generated by FINFO()
SAS Documentation on NLDATM informat
Example program that uses NLDATM informat to get datetime from FINFO() function.
Use a different in-format
37 data _null_;
38 x = '12 January 2023 11:03:28';
39 moddate=input(x,datetime20.);
40 moddate2=input(x,anydtdtm30.);
41 put 'NOTE: ' (mod:)(=datetime.);
42 run;
NOTE: Invalid argument to function INPUT at line 39 column 12.
NOTE: moddate=. moddate2=12JAN23:11:03:28
I have a column called month as
month
JAN
FEB
...
DEC
I'd like to know how to convert them into 1,2,3,...,12 in SAS. Thanks a lot.
Use informat to convert it to number and use month() to get the month.
data have;
input month :$3. ##;
datalines;
JAN FEB DEC
;
data want;
set have;
x=month(input(month||'21',??monyy.));
run;
Concatenate the month with a year, make use of the MONYY. informat, use the MONTH. format and finally output as a numeric value using another input().
data have;
input month :$3. ##;
datalines;
JAN FEB DEC
;
data want;
set have;
month_num=input(put(input(catt(month, year(today())), monyy.), month.), 2.);
put month month_num;
run;
Results:
JAN 1
FEB 2
DEC 12
I have a data in format 01 Jan 19.00.00 (datetime), and I want to extract the month name only from it.
Tried the below code but getting output in numbers i.e. 1 2 3 and so on. I want the output either in Jan Feb mar format or January February march format.
data want;
set detail;
month = month(datepart(BEGIN_DATE_TIME));
run;
You can use the MONNAME format.
data test;
dt = datetime();
monname = put(datepart(dt),MONNAME.);
put monname=;
run;
If you want "OCT" not "OCTOBER" you can add a 3 to the format (MONNAME3.).
If you are using the value in a report the better approach might be to use a date value formatted with MONNAME.
The values of a date formatted variable will be ordered properly when the variable is used in a CLASS or BY statement. If you had instead computed a new variable as the month name, the default ordering of values would be alphabetical.
data want;
set have;
begin_date = datepart(BEGIN_DATE_TIME);
format begin_date MONNAME3.;
run;
I have variables day,month and year, month is string and day,year is numeric, what is the fastest way to convert this into the correct date format (dmy)? I know I can write code converting the month into numeric month (might be little long) and then use mdy() function, is there a faster way ? or make use of the original month and year to create a mdy format?
I appreciate any suggestion
Consider concatenating variables (converting numeric ones to string) and then converting to DMY:
Input Data
month day year
January 21 2016
February 13 2016
March 6 2016
Stata script
gen fulldate = date(string(day) + "" + month + "" + string(year), "DMY")
format fulldate %td
Result
month day year fulldate
January 21 2016 21jan2016
February 13 2016 13feb2016
March 6 2016 06mar2016
I'm trying to delete all the rows that are in the BATCH: May-2014.
data out
set INPUT;
if MONTH(BATCH) NE 05 and YEAR(BATCH) NE 2014;
RUN;
Data in Batch column is Numeric in the format MONYY5.
EX:::: MAR13, APR14, MAY14, FEB14, JAN14, FEB12
After I run the code it is deleting all 2014 records instead of deleting MAY and 2014.
Thanks in advance.
Because you're asking for everythign that is neither 2014 nor 05. You want everything that is not (both 2014 and 05).
data out
set INPUT;
if NOT (MONTH(BATCH) eq 05 and YEAR(BATCH) eq 2014);
RUN;
Another option if you know it's MONYY:
data out
set INPUT;
if vvalue(batch) ne 'MAY14'; *vvalue gives formatted value
RUN;
Only works if you're sure it's formatted that way, though.