data dates; input orderdate; datalines; 1 24FEB03:00:00:00 2 05JUL03:00:00:00 3 07JAN03:00:00:00 4 25AUG03:00:00:00 5 10OCT03:00:00:00 6 28OCT03:00:00:00 7 11NOV03:00:00:00 8 18NOV03:00:00:00 9 12JAN03:00:00:00 10 15JAN04:00:00:00 ; run;
data n;
set dates;
if orderdate=input(month(orderdate),8.);
run;
Hi Guys ,
above dataset i want pull month of JAN using where or if clause for numeric date format please fix datatype error
Try this:
data dates;
input id 2. orderdate :date7.;
datalines;
1 24FEB03:00:00:00
2 05JUL03:00:00:00
3 07JAN03:00:00:00
4 25AUG03:00:00:00
5 10OCT03:00:00:00
6 28OCT03:00:00:00
7 11NOV03:00:00:00
8 18NOV03:00:00:00
9 12JAN03:00:00:00
10 15JAN04:00:00:00
;
data want;
set dates;
if month(orderdate)=1;
run;
First, please note that your INPUT statement the way you wrote it won't work, and you're not even close. You have two columns of data, but you are trying to read in the ORDERDATE from the first column. You MUST read in ORDERDATE from the second column, as a date value (or datetime value), which can be done in this case using the DATE7. informat.
Your IF statement isn't even close either, you don't specify in the IF statement that you want January, so it will never give you just the January data.
data dates;
input rownumber orderdate : datetime16.0;
format orderdate datetime22.3;
datalines;
1 24FEB03:00:00:00
2 05JUL03:00:00:00
3 07JAN03:00:00:00
4 25AUG03:00:00:00
5 10OCT03:00:00:00
6 28OCT03:00:00:00
7 11NOV03:00:00:00
8 18NOV03:00:00:00
9 12JAN03:00:00:00
10 15JAN04:00:00:00
;
run;
data n;
set dates;
if month(datepart(orderdate))=1 then output;
run;
/* end of program */
Or like this :
data dates;
input rownumber orderdate $20.;
datalines;
1 24FEB03:00:00:00
2 05JUL03:00:00:00
3 07JAN03:00:00:00
4 25AUG03:00:00:00
5 10OCT03:00:00:00
6 28OCT03:00:00:00
7 11NOV03:00:00:00
8 18NOV03:00:00:00
9 12JAN03:00:00:00
10 15JAN04:00:00:00
;
run;
data n;
set dates;
if month(datepart(input(orderdate,datetime16.)))=1 then output;
*if index(orderdate,'JAN') > 0 then output;
run;
/* end of program */
Koen
Hi sbxkoenk
Thank you for your solution
Try this:
data dates;
input id 2. orderdate :date7.;
datalines;
1 24FEB03:00:00:00
2 05JUL03:00:00:00
3 07JAN03:00:00:00
4 25AUG03:00:00:00
5 10OCT03:00:00:00
6 28OCT03:00:00:00
7 11NOV03:00:00:00
8 18NOV03:00:00:00
9 12JAN03:00:00:00
10 15JAN04:00:00:00
;
data want;
set dates;
if month(orderdate)=1;
run;
First, please note that your INPUT statement the way you wrote it won't work, and you're not even close. You have two columns of data, but you are trying to read in the ORDERDATE from the first column. You MUST read in ORDERDATE from the second column, as a date value (or datetime value), which can be done in this case using the DATE7. informat.
Your IF statement isn't even close either, you don't specify in the IF statement that you want January, so it will never give you just the January data.
Ok Miller
thank you for your solution can i write code to extract only month name
data n;
set country_sales;
if month(datepart(substr(orderdate,2,3))) in (1,4,10) then output;
run;
2. in this case where statement cannot filter datetime format?
Yes, you can write such code. If you are using the code I provided earlier, SUBSTR and DATEPART are not needed and are wrong. DATEPART not needed because my code makes ORDERDATE a date variable, not a datetime variable. I showed code how to get January without SUBSTR and DATEPART, why would you add in SUBSTR and DATEPART to get other months?
in this case where statement cannot filter datetime format?
sure it can.
Since we've told you this already (and many times on top), answer these questions:
One of the above questions is, of course, a catch (or trick) question.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.