BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
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 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

7 REPLIES 7
sbxkoenk
SAS Super FREQ
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 */
sbxkoenk
SAS Super FREQ

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

BrahmanandaRao
Lapis Lazuli | Level 10

Hi sbxkoenk

Thank you for your solution

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
BrahmanandaRao
Lapis Lazuli | Level 10

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?

 

 

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Kurt_Bremser
Super User

Since we've told you this already (and many times on top), answer these questions:

  • Is a SAS datetime value a number or character?
  • If it is a number, what does the number count?
  • If it is a string, how is the date and time coded into it?

One of the above questions is, of course, a catch (or trick) question.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 856 views
  • 0 likes
  • 4 in conversation