Help using Base SAS procedures

How to sort by date and create two new data sets.

Reply
Occasional Contributor
Posts: 5

How to sort by date and create two new data sets.

Hello,

 

I am in need of some help.

 

I have a data set that that contains 7000 observations and one of the variables is wear-date. Wear date specifies the date the data was recorded. The format for wear date is (i.g. 11/9/2014).

 

I would like to create two data sets from one main data set. One for weekday observations and one for weekend observations. Therefore I need to sort dates by weekday and weekends.

 

I was told to use the "weekday function", however, I am not sure if this is in a data or proc step.

 

Any help would be appreciated, I am a beginner, and I am using SAS studio. Thank you. 

 

Below is an example data set.

 

ID      wearDate   sed    wearTime (MIN)
281    11/9/14    667    984
282   11/10/14    792    1009
283    11/11/14    674    875
284    11/12/14    785    1029
285    11/13/14    684    909
286    11/14/14    662    878

 

 

 

Super User
Posts: 23,700

Re: How to sort by date and create two new data sets.

Posted in reply to ebonfil89

Functions are used in a data step or PROC SQL, not in other PROCS.  

 

 

If you review the documentation for WEEKDAY() it returns 1-7, with 1/7 being weekends so you can classify your dates. Then you can separate them. I'm not sure why you need to sort. 

 

1. Create a new variable using the WEEKDAY function to get the weekday

2. Create another new variable that shows if it's a weekend or weekday

 

Why do you need separate data steps? It's unlikely you actually do need a separate data sets.

 

data identify_weekday;

set have; *this should be your input data set;

weekDayNumber = weekday(date); *get weekday;

If weekDayNumber in (1, 7) then weekDayType='Weekend';
else if weekDayNumber in (2:6) then weekDayType='Weekday';
else weekDayType='CHECKME';

run;

*Check coding is correct;
proc freq data=identify_weekday;
table weekDayType*weedDayNumber;
run;

*See summaries by weekdayType;
proc means data=identify_weekday;
class weekDayType;
var wearTime;
run;

ebonfil89 wrote:

Hello,

 

I am in need of some help.

 

I have a data set that that contains 7000 observations and one of the variables is wear-date. Wear date specifies the date the data was recorded. The format for wear date is (i.g. 11/9/2014).

 

I would like to create two data sets from one main data set. One for weekday observations and one for weekend observations. Therefore I need to sort dates by weekday and weekends.

 

I was told to use the "weekday function", however, I am not sure if this is in a data or proc step.

 

Any help would be appreciated, I am a beginner, and I am using SAS studio. Thank you. 

 

Below is an example data set.

 

ID      wearDate   sed    wearTime (MIN)
281    11/9/14    667    984
282   11/10/14    792    1009
283    11/11/14    674    875
284    11/12/14    785    1029
285    11/13/14    684    909
286    11/14/14    662    878

 

 

 


 

Ask a Question
Discussion stats
  • 1 reply
  • 101 views
  • 0 likes
  • 2 in conversation