I was extracting Year, Month Name and Day Name from Date column. But the problem that I faced during the extraction of day name. In result till 31-01-2022 day name is showing correctly and when 01-02-2022 starts wrong day name is getting printed it continues till 31-01-2023 which is the last date in my dataset.
For Example on 01-02-2022 day should be Tuesday but result I am getting is Saturday.
Contents of table
Snapshot of result of I am getting
My Code:
DATA formated_date;
SET date;
Year = YEAR(Date);
Month_Name = PUT(Date, MONNAME.);
Day_Name = DAY(Date);
FORMAT Day_Name downame9.;
RUN;
Can anyone please tell me why I am getting wrong results?
It would be helpful if you could show us (a portion of) the data in SAS data set DATE, along with the PROC CONTENTS of this data set.
However, as a guess, I think you want this, as the DOWNAME format applies to SAS date values, not to the day number extracted to a variable.
Day_Name = put(date,downame9.);
although I don't recommend doing it this way. You don't need to store the day_name as a character string at all, just leave it as a date, and apply formats to it as needed.
It would be helpful if you could show us (a portion of) the data in SAS data set DATE, along with the PROC CONTENTS of this data set.
However, as a guess, I think you want this, as the DOWNAME format applies to SAS date values, not to the day number extracted to a variable.
Day_Name = put(date,downame9.);
although I don't recommend doing it this way. You don't need to store the day_name as a character string at all, just leave it as a date, and apply formats to it as needed.
Hello,
Thank You for your reply @PaigeMiller .
As you suggested, I updated my post by attaching POC CONTENTS and results snapshots.
So @ketan_korde this brings up the question ... why do you need YEAR in its own variable, why do you need MONTH in its own variable, and why do you need DAY in its own variable? Usually these steps are not needed, and you can just work with the SAS date value ... please explain why you are doing this, how will these new variables be used?
Hello @PaigeMiller , I recently started learning SAS Programming Language and was practicing different date functions and formats.
@ketan_korde wrote:
Hello @PaigeMiller , I recently started learning SAS Programming Language and was practicing different date functions and formats.
Okay, that's a good reason to learn how to use the formats on date values, but just so that you know, in most cases you are better off working with the actual date value, rather than putting "Wednesday" into a new variable, and also having "March" in a new variable. This is unnecessary and inefficient programming in most cases. Even if you wanted to compute averages by day of week, you can do this on the DATE value, without storing "Monday" "Tuesday" etc in its own variable.
proc means data=fake_dates;
var date;
format date downame.;
run;
Ideally, you apply a format to the date, rather than create a new variable.
You did the right thing for the month name. Generated the name using a date format.
But you did the wrong thing for the day of the week name. You converted the date into a number between 1 and 31 and then formatted using a date format. So you get the day of the week for a date between 02JAN1960 and 01FEB1960 instead of for the actual date.
Also the MONNAME and DOWNAME formats are weird. They will right align by default. So use the -L format modifier to avoid having values with leading spaces.
data dates_as_strings;
today=date();
format today date9.;
year= put(today,year4.);
month=put(today,monname.-L);
day_of_week=put(today,downame.-L);
put today= (_character_) (/=$quote.);
run;
today=04JAN2024 year="2024" month="January" day_of_week="Thursday"
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.