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
;
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;
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;
data want;
set HealthHosp;
where strip(put(weekday(VisitD),weekdate9.))="Monday" or year(visitd)=2002;
run;
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.
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
;
@Astounding, Good Catch 🙂
YEAR/MONTH/DAY functions.
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.
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.