DATA Step, Macro, Functions and more

how to find current week in SAS

Reply
Frequent Contributor
Posts: 84

how to find current week in SAS

Hi SAS Users,

 

I wanted some help in finding the current week logic like below.  

 

Logic - get the current week from the Modiefied date and compare it with the current week.

 

    where week(datepart(modified_date)) eq week(today()) 

 

 

data current_week;
set previous_dataset;               ---------->  it has a field  'modified_date'  - a date field.
where week(datepart(modified_date)) eq week(today());
run;

 

Thanks,

Ana

Trusted Advisor
Posts: 1,615

Re: how to find current week in SAS

Exactly what help do you want?

 

Please explain what is not working. Show us a portion of your data. Show us the relevant code and relevant parts of the SASLOG.

Super User
Posts: 5,083

Re: how to find current week in SAS

First a couple of general guidelines ...

 

If you have a date variable, you would not apply DATEPART to it.  DATEPART only applies to a variable that contains a datetime value, to extract the date.

 

The WEEK function finds the week within the year.  So if you compare the WEEK of TODAY() to the WEEK of some other date variable, you could find a match for dates that are a year apart.

 

What is in your existing modified date variable?  Data for every day, or just data for one day per week?  It is conceivable you could use something as simple as:

 

where modified_date > today() - 7;

Super User
Posts: 17,836

Re: how to find current week in SAS

@SASAna not seeing anything wrong with your code so it's a logic error. To clarify we'll need to see what the variable looks like, what the type is and are you absolutely sure you have data for this week already?

Frequent Contributor
Posts: 84

Re: how to find current week in SAS

Requirment is to get the current week's work assignments by using the modified_date field.

 

modified_date is datetime field. So i have used datepart function.

 

data current_week;
set previous_dataset;               
where datepart(modified_date) le today() - 7 ;
run;

 

Thanks,

Ana

Super User
Posts: 5,083

Re: how to find current week in SAS

So that's most of the way there.   The comparison "le" is incorrect, as it selects the wrong time period.  Replace it with either > or >=  depending on whether the date from 7 days earlier should be excluded or included.

Ask a Question
Discussion stats
  • 5 replies
  • 84 views
  • 0 likes
  • 4 in conversation