How to populate weekday from date (MMDDYYYY) in SAS?

sas
data_wrangling

#1

Hi,

I am working on a project and during data preparation process, I need to create a variable based on other variable. In data set, source variable is Hour and values stored under this field as 11012013 (MMDDYYYY) but this is stored as number. Now I want to populate weekday from this variable Hour. Is there any function to perform this? Can you please help me?

Thanks, Steve


#2

Hi Steve,

First you need to convert this string into SAS date format. Post that you can use a “Weekday” function to extract weekday information from it. Use the below link for help on date/time formats in SAS -

Hope this helps.

Regards,
Aayush


#3

@Steve
You can perform mathematical calculations to extract month, day and year from this numeric variable and create date based on these. After this use Weekday() function to populate weekday of the given date.

Data Test;
Input Hour;
datalines;
11012013
12012013
13012013
;
Run;

Data Check;
Set Test;
Year=Hour-Int(Hour/10000)*10000;
Day=Int(Hour/1000000);
Month=Int(Hour/10000)-Day*100;
T_Date=MDY(Month,Day,Year);
T_Weekday=Weekday(T_Date);
Run;

Regards,
Sunil