DATA Step, Macro, Functions and more

How do you isolate Month, Day, or Year from a Date?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 121
Accepted Solution

How do you isolate Month, Day, or Year from a Date?

Hi,

 

I am trying to create a second data set which finds when the day of the VisitD variable  is a monday or the year of the VisitD variable is 2002. I am using the Set function. I am learning from SAS by Example and the answers are not in the book. Could someone offer some assistance? Thanks.

 

Below is the original dat set from which I want to find which dates fall on a Monday or are in the year 2002, using SAS code.

 

Libname SASdata '/folders/myfolders/SASData' ;
Data SasData.HealthHosp ;
    Input PatID 3.
          DOB   mmddyy10.
          VisitD mmddyy10. ;
    format DOB VisitD mmddyy10. ;
Datalines ;
00112/25/19445/17/2000
00205/11/19668/4/2002
00308/03/20009/2/2003
;


Accepted Solutions
Solution
‎09-08-2017 06:01 PM
PROC Star
Posts: 1,193

Re: How do you isolate Month, Day, or Year from a Date?

[ Edited ]
Posted in reply to ManitobaMoose

Like this?

 

Data HealthHosp ;
    Input PatID 3.
          DOB   mmddyy10.
          VisitD mmddyy10. ;
    format DOB VisitD mmddyy10. ;
Datalines ;
00112/25/19445/17/2000
00205/11/19668/4/2002
00308/03/20009/2/2003
;

data want;
   set HealthHosp;
   where weekday(VisitD)=2 | year(VisitD)=2002;
run;

View solution in original post


All Replies
Solution
‎09-08-2017 06:01 PM
PROC Star
Posts: 1,193

Re: How do you isolate Month, Day, or Year from a Date?

[ Edited ]
Posted in reply to ManitobaMoose

Like this?

 

Data HealthHosp ;
    Input PatID 3.
          DOB   mmddyy10.
          VisitD mmddyy10. ;
    format DOB VisitD mmddyy10. ;
Datalines ;
00112/25/19445/17/2000
00205/11/19668/4/2002
00308/03/20009/2/2003
;

data want;
   set HealthHosp;
   where weekday(VisitD)=2 | year(VisitD)=2002;
run;
PROC Star
Posts: 1,357

Re: How do you isolate Month, Day, or Year from a Date?

[ Edited ]

data want;
set HealthHosp;
where strip(put(weekday(VisitD),weekdate9.))="Monday" or year(visitd)=2002;
run;

Frequent Contributor
Posts: 121

Re: How do you isolate Month, Day, or Year from a Date?

Grat, thanks. One quick follow up: I thought for the Weekday function Sunday = 1 and Monday = 2? That is what the book says, but maybe that has changed since?

 

Thanks.

Super User
Posts: 6,544

Re: How do you isolate Month, Day, or Year from a Date?

Good set of tools, but notice ...

 

For Monday, the WEEKDAY function should return 2 not 1.  Also note, if you are reading from the raw data, you can build in efficiency (not a consideration when you have only a few lines of raw data):

 

Data want ;
    Input @ 14 VisitD mmddyy10. @ ;
if weekday(VisitD) = 2 or year(VisitD) = 2002;
Input @ 1 PatID 3. DOB mmddyy10. ; format DOB VisitD mmddyy10. ; Datalines ; 00112/25/19445/17/2000 00205/11/19668/4/2002 00308/03/20009/2/2003 ;

 

PROC Star
Posts: 1,193

Re: How do you isolate Month, Day, or Year from a Date?

Posted in reply to Astounding

@Astounding, Good Catch Smiley Happy

Super User
Posts: 22,875

Re: How do you isolate Month, Day, or Year from a Date?

Posted in reply to ManitobaMoose

 

YEAR/MONTH/DAY functions.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 291 views
  • 1 like
  • 5 in conversation