BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ketan_korde
Fluorite | Level 6

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 tableContents of table

 

Snapshot of result of I am gettingSnapshot 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?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ketan_korde
Fluorite | Level 6

Hello,

 

Thank You for your reply @PaigeMiller .

 

As you suggested, I updated my post by attaching POC CONTENTS and results snapshots.

PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
ketan_korde
Fluorite | Level 6

Hello @PaigeMiller , I recently started learning SAS Programming Language and was practicing different date functions and formats.

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
ketan_korde
Fluorite | Level 6
Okay, I will keep this in mind while working on data.

Thank you for the notes.
Tom
Super User Tom
Super User

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"

 

ketan_korde
Fluorite | Level 6

Hii @Tom , Thank you for your reply.

 

Today I learned something new.

 

Thanks a lot.

sas-innovate-2024.png

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
  • 8 replies
  • 764 views
  • 1 like
  • 3 in conversation