DATA Step, Macro, Functions and more

How to find a particular date from a dataset

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How to find a particular date from a dataset

Hi,

 

I am trying to fetch the id's who transacted money on 25thDec2016 only.

 

id dateTransaction_Amount
125-Dec-162300
216-May-163000
31-Jan-161200
425-Dec-164000
531-Oct-169000
625-Sep-161700
725-Dec-16800
819-Mar-16500
99-Jul-162000
1025-Dec-161500

 

Please help!!


Accepted Solutions
Solution
‎05-15-2017 11:03 PM
PROC Star
Posts: 763

Re: How to find a particular date from a dataset

Posted in reply to anilatikkha310

You can extract the relevant observations like this

 

data have;
input id $ date :date7. Transaction_Amount;
format date date7.;
datalines;
1 25-Dec-16	2300
2 16-May-16	3000
3 1-Jan-16 1200
4 25-Dec-16	4000
5 31-Oct-16	9000
6 25-Sep-16	1700
7 25-Dec-16	800
8 19-Mar-16	500
9 9-Jul-16 2000
10	25-Dec-16 1500
;

data want;  
   set have;
   where date = '25DEC16'd;
run;

Is that what you want?

View solution in original post


All Replies
PROC Star
Posts: 763

Re: How to find a particular date from a dataset

Posted in reply to anilatikkha310

How is your data stored? Is date stored as a character or numeric variable?

New Contributor
Posts: 3

Re: How to find a particular date from a dataset

The date is stored in numeric format.
##- Please type your reply above this line. Simple formatting, no
attachments. -##
Solution
‎05-15-2017 11:03 PM
PROC Star
Posts: 763

Re: How to find a particular date from a dataset

Posted in reply to anilatikkha310

You can extract the relevant observations like this

 

data have;
input id $ date :date7. Transaction_Amount;
format date date7.;
datalines;
1 25-Dec-16	2300
2 16-May-16	3000
3 1-Jan-16 1200
4 25-Dec-16	4000
5 31-Oct-16	9000
6 25-Sep-16	1700
7 25-Dec-16	800
8 19-Mar-16	500
9 9-Jul-16 2000
10	25-Dec-16 1500
;

data want;  
   set have;
   where date = '25DEC16'd;
run;

Is that what you want?

Super User
Posts: 19,832

Re: How to find a particular date from a dataset

Posted in reply to anilatikkha310

If the field is a SAS date, numeric with a date format then you need a date literal.

 

Date Literal (d)

When hard coding in a date into your code or creating a date, you need to specify it in the DATE9. format, and add a d at the end. For example:

date_var = ’31Jan2016’d;

You aren’t required to put in a 4 year date, but I highly recommend it. Another way to reference a date without using a literal is to use the MDY function. 1,

x = MDY(1, 31, 2016);

With the dates specified in this format you can use it in your code, either assign it to a variable or use it for comparison. The following two lines of code will result in the same thing

if x=’31Jan2016’d then do;

if x=mdy(1, 31, 2016) then do;

New Contributor
Posts: 3

Re: How to find a particular date from a dataset

Posted in reply to anilatikkha310
Thanks for explaining the above cases it is really informative.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 156 views
  • 3 likes
  • 3 in conversation