BookmarkSubscribeRSS Feed
Calcite | Level 5

Hey All,

I am trying to write a code in SAS where from

Tuesday to Friday, it gets data from previous day (Trans_Date is the variable) and on Monday, I want it to get all three days data from the weekend. Appreciate all the help I get.

Super User

WEEKDAY() function tells you what day of the week it is.

Super User

First thing is to make sure that you have SAS date valued variables.

Then there are several date related functions that may help.


To get specific examples you need to provide data that you have now and what you expect.

Instructions here: will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

Calcite | Level 5
This is my SQL code that I need to change it to SAS
select * into #switch4 from #TransactionsA where trans_date2>= dateadd(dd, case
when 'MON'
then -3
else -1 end
,cast(cast(getdate() as date) as datetime))
Quartz | Level 8

Another way to approach this is to maintain a dataset that has a single value...the date it last ran. You then pass that value into a start_date variable that can be used in your program. At the very end, you overwrite the dataset with today's date, that becomes the start date, the next time you run it. This way, if you happen to have a day it can't run during the week, you pick up that data the next day it does run. 

libname mydata "directory_path";
data _null_;
set mydata.last_run_date;
call symputx('start_date',"'"||put(date_last_run,date9.)||"'d");

data want;
set have;
where date_var >= &start_date. and date_var < date();

data mydata.last_run_date;


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.


Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 4 in conversation