Hi I have reading a CSV file in SAs, and the CSv file has this date format:
2020-12-30 07:40:00
It says it has a format $21, character when I click on the variable.
I want to make a statment in a datastep where I only have the dates from the today()-3 and today()-9
or data between 2021-01-10 and 2021-01-16
how can I do that with this kind of format
One could argue that today() - 3 was the 15. 🙂
However, yes do like this
data have;
input dt $20.;
datalines;
2020-12-28 07:40:00
2020-12-29 07:40:00
2020-12-30 07:40:00
2020-12-31 07:40:00
2021-01-01 07:40:00
2021-01-02 07:40:00
2021-01-03 07:40:00
2021-01-04 07:40:00
2021-01-05 07:40:00
2021-01-06 07:40:00
2021-01-07 07:40:00
2021-01-08 07:40:00
2021-01-09 07:40:00
2021-01-10 07:40:00
2021-01-11 07:40:00
2021-01-12 07:40:00
2021-01-13 07:40:00
2021-01-14 07:40:00
2021-01-15 07:40:00
2021-01-16 07:40:00
2021-01-17 07:40:00
2021-01-18 07:40:00
;
data want;
set have;
dtnum = input(dt, anydtdtm19.);
datepart = datepart(dtnum);
if today()-8 <= datepart <= today()-3;
format dtnum datetime20. datepart ddmmyy10.;
run;
Result:
dt dtnum datepart 2021-01-10 07:40:00 10JAN2021:07:40:00 10/01/2021 2021-01-11 07:40:00 11JAN2021:07:40:00 11/01/2021 2021-01-12 07:40:00 12JAN2021:07:40:00 12/01/2021 2021-01-13 07:40:00 13JAN2021:07:40:00 13/01/2021 2021-01-14 07:40:00 14JAN2021:07:40:00 14/01/2021 2021-01-15 07:40:00 15JAN2021:07:40:00 15/01/2021
How did you import the CSV file? If you do so with a data step and the infile statement, you can specify exactly how the variable should be read.
In this case, a quick fix would be to use the input function and convert the character datetime to a numeric SAS Datetime variable and find the date part like this. Hope this helps.
data test;
dt = "2020-12-30 07:40:00";
dtnum = input(dt, anydtdtm19.);
datepart = datepart(dtnum);
format dtnum datetime20. datepart ddmmyy10.;;
run;
I have a 1000 different date in that format, will it change them all with this code? :9
Do you have 1000 date variables or 1000 observations with date values stored as character?
I have one variable of dates but ,many observations
Then yes. Simply do like below. I just created three obs for demonstration.
data have;
input dt $20.;
datalines;
2020-12-28 07:40:00
2020-12-29 07:40:00
2020-12-30 07:40:00
;
data want;
set have;
dtnum = input(dt, anydtdtm19.);
datepart = datepart(dtnum);
format dtnum datetime20. datepart ddmmyy10.;;
run;
Thank you is it possible to make a statment
where it only outputs dates from the today()-8 which is the 10th january to today()-3 which is te 16th january
One could argue that today() - 3 was the 15. 🙂
However, yes do like this
data have;
input dt $20.;
datalines;
2020-12-28 07:40:00
2020-12-29 07:40:00
2020-12-30 07:40:00
2020-12-31 07:40:00
2021-01-01 07:40:00
2021-01-02 07:40:00
2021-01-03 07:40:00
2021-01-04 07:40:00
2021-01-05 07:40:00
2021-01-06 07:40:00
2021-01-07 07:40:00
2021-01-08 07:40:00
2021-01-09 07:40:00
2021-01-10 07:40:00
2021-01-11 07:40:00
2021-01-12 07:40:00
2021-01-13 07:40:00
2021-01-14 07:40:00
2021-01-15 07:40:00
2021-01-16 07:40:00
2021-01-17 07:40:00
2021-01-18 07:40:00
;
data want;
set have;
dtnum = input(dt, anydtdtm19.);
datepart = datepart(dtnum);
if today()-8 <= datepart <= today()-3;
format dtnum datetime20. datepart ddmmyy10.;
run;
Result:
dt dtnum datepart 2021-01-10 07:40:00 10JAN2021:07:40:00 10/01/2021 2021-01-11 07:40:00 11JAN2021:07:40:00 11/01/2021 2021-01-12 07:40:00 12JAN2021:07:40:00 12/01/2021 2021-01-13 07:40:00 13JAN2021:07:40:00 13/01/2021 2021-01-14 07:40:00 14JAN2021:07:40:00 14/01/2021 2021-01-15 07:40:00 15JAN2021:07:40:00 15/01/2021
A simple condition:
where today() - 8 le date le today() - 3
either as part of a SQL SELECT, or as statement in a data step.
Thanks erveryone :9
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.