BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mmea
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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 

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

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;
mmea
Quartz | Level 8

I have a 1000 different date in that format, will it change them all with this code? :9

PeterClemmensen
Tourmaline | Level 20

Do you have 1000 date variables or 1000 observations with date values stored as character? 

mmea
Quartz | Level 8

I have one variable of dates but ,many observations

PeterClemmensen
Tourmaline | Level 20

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;
mmea
Quartz | Level 8

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

PeterClemmensen
Tourmaline | Level 20

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 
mmea
Quartz | Level 8

Thanks erveryone :9

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 9 replies
  • 3699 views
  • 0 likes
  • 3 in conversation