How can I convert dd/mm/yy/ to yy/mm/dd in SAS
I have
ACCESS_DATE was something like: 18012019, but needs to be 2019/01/18
I did the following:
,put(ACCESS_DATE, YYMMDDS10.) as 'ACCESS_DATE'n
but this creates: 18/01/2019
So how do I get the yyyy in the front like: yy/mm/dd in SAS EG
In EG you might be selecting the format from a drop down list. Double check the variable type (num or char) and format (several with y, m & d).
data _null_;
date = '18-JAN-2019'd;
put date #15 'SAS date value';
put date date9. #15 'DATE9. format';
put date date11. #15 'DATE11. format';
put date ddmmyyn8. #15 'DDMMYYN8. format';
put date YYMMDDS10. #15'YYMMDDS10. format';
put date MMDDYYS10. #15'MMDDYYS10. format';
put date DDMMYYS10. #15'DDMMYYS10. format';
run;
----- LOG -----
21567 SAS date value
18JAN2019 DATE9. format
18-JAN-2019 DATE11. format
18012019 DDMMYYN8. format * possibly what you were seeing initially
2019/01/18 YYMMDDS10. format * what is wanted
01/18/2019 MMDDYYS10. format
18/01/2019 DDMMYYS10. format * only way to get what is stated in question
Related
Want to convert date = 01-SEP-2021 to date = 01SEP2021. I used this code to convert:
New date = input(compress(date,'-'),date9.);
But that didn't work. I also used substring to remove the parts of day month and year but seems this method is a bit lengthy.
If the type is numeric and SAS already displays it as a date then it's a formatting issue.
proc datasets lib=work nolist;
modify have;
format date date9.;
quit;
Otherwise, it must be a character and you have to convert it to a date and display it in date9. format.
data want;
date = '01-SEP-2021';
new_date = input(date, anydtdte.); *anydtdte. automatically reads a variety of date formats;
format new_date date9.;
run;
I need to input dates in YYYYMMDD format and create macro variables from these dates to use in a WHERE clause. The FINAL dataset should select one record from Sales but 0 observations are returned.
data work.FiscalYear2019;
input #1 fiscalYear $4. #5 StartDate mmddyy8.;
retain diff;
if fiscalYear = '2019' then do;
tday = today();
diff = tday - StartDate;
call symputx('FYTD_days',diff);
call symputx('CY_StartDate', StartDate);
call symputx('CY_EndDate', put(today(),mmddyy8.));
end;
else if fiscalYear = '2018' then do;
PY_EndDate = StartDate + diff;
call symput('PY_EndDate', put(PY_EndDate,mmddyy8.));
call symput('PY_StartDate', put(StartDate,mmddyy8.));
end;
datalines;
201912312018
201801012018
;
data work.Sales;
input #1 fiscalYear $4. #5 orderDate mmddyy8.;
format orderDate mmddyy6.;
datalines;
201902042019
201801012018
;
data final (WHERE=(orderDate >= &PY_StartDate AND
orderDate <= &PY_EndDate));
set Sales;
run;
I expect the FINAL dataset to contain one record from the Sales dataset but FINAL has 0 observations.
To use your macro variables as date values you need to either generate the macro variables as the raw number of days values, like you did with CY_StartDate, or generate them using the DATE format and enclose them in quotes and append the letter D to make a date literal.
Like this:
call symputX('PY_StartDate', put(StartDate,date9.));
call symputX('PY_EndDate', PY_EndDate);
...
data final
set Sales;
WHERE orderDate >= "&PY_StartDate"d
AND orderDate <= &PY_EndDate
;
run;
Also your subject line mentions YYYYMMDD informat and it does not appear in your code. Are you interpreting your source data properly? Does 201801012018 represent an 8 digit date in YMD order plus a four digit year? Or a four digit year plus an 8 digit date in MDY order?
You are just not referring to your macro variables in the last data step with the proper syntax. Those &PY_StartDate and &PY_EndDate variables are just strings after macro code is compiled, and you need to refer to them as date constants. So this should fix the issue:
data final (WHERE=(orderDate >= "&PY_StartDate"d AND
orderDate <= "&PY_EndDate"d));
set Sales;
run;
In the future, I recommend including options mprint; at the start of your code. This option displays the text generated by macro execution in your log, which can help greatly with debugging macros.
I have a SAS field where the datatype is number and format is date9.
It has a value like 30SEP2018.
How do I convert it to a SAS date so I can do date operations?
SAS dates are stored as number starting from 1/1/1960 and it starts form number = 0 and increases by 1 for every day. Your date is stored as number and then you get from proc contents and format is to display in the way you want.
data have;
input date:date9.;
format date date9.;
datalines;
30SEP2018
;
proc contents data=have;
run;
you can calculations on above date and gives you appropriate results as shown below
data want;
set have;
new_date= date+1;
new_date1= date-1;
format new_date new_date1 date9.;
run;
proc print; run;
I would like to get only date and time separately from 01JAN13:08:29:00
Format & Infomat available in Dataset is:
Date Num 8 DATETIME.(format) ANYDTDTM40(informat)
And If I run datepart() on 01JAN13:08:29:00 I get output as 19359 (I don't want it.)
The DATEPART function extracts the date value from a datetime value. The date value as you have seen is simply a number. A date format must be applied to a variable holding a date value. Base SAS variables have only two value types, character and numeric.
data want;
now_dtm = datetime();
now_dt = datepart(now_dtm);
now_dt_unformatted = now_dt;
format now_dtm datetime.;
format now_dt date9.; * <----- this is what you need, format stored in data set header information;
run;
proc print data=want;
run;
* you can change the format temporarily during a proc step;
proc print data=want;
format now_dt yymmdd10.; * <---- changes format for duration of proc step;
format now_dt_unformatted mmddyy10.;
run;
Actually 19,359 is exactly the value you want. You started with the number of seconds since 1960 and converted it to the number of days since 1960.
data x ;
dt = '01JAN13:08:29:00'dt ;
date = datepart(dt);
time = timepart(dt);
put (dt date time) (=);
run;
Results
dt=1672648140 date=19359 time=30540
You just need to attach a format to your new variable so that SAS will display the value in a format that humans will recognize. You could use a format like DATE9. to have it show 19,359 as 01JAN2013. Similarly you need to attach a format to the time part to make it print in format that human's will interpret as a time.
format date date9. time time8. ;
I have a table in SAS where in one column, the date is stored (e.g. "2005/10").
How do I have to convert this to a SAS data format?
Among many other tries, I tried this code:
data test;
format date YYMMS.;
date = input(ObservationMonth, YYMMS.);
put date=date9.;
run;
You could just use the anydtdte. informat.
data want;
format date yymms.;
text="2005/10";
date=input(text,anydtdte.);
put date;
run;
This informat detects most date formattings and converts the character-stored value to a SAS date.
One way is to use substr() and mdy() to extract the date components:
data _null_;
ObservationMonth ="2005/10";
date =mdy(substr(ObservationMonth,6,2),1,substr(ObservationMonth,1,4));
put date = date9.;
run;
Another option is to use the anydtdte informat (note that results may differ depending on your locale):
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth,anydtdte.);
put date = date9.;
run;
Yet another option is to modify the input to enable use of the YYMMDDw. informat:
data _null_;
ObservationMonth ="2005/10";
date =input(ObservationMonth!!'/01', YYMMDD10.);
put date = date9.;
run;
you're fantastic, guys! Thank you so much, with a "set" statement it works fine!