BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
urban58
Quartz | Level 8

Hello SAS community,

I have the following   

examdate

08/25/2020

09/01/2020

.

.

09/14/2020

10/01/2020

 

 

I want to extract year and month, if examdate is missing then year and month variable (newdate)=202009 so I get the following

 

examdate newdate

08/25/2020 202008

09/01/2020 202009

.                  202009

.                  202009

09/14/2020 202009

10/01/2020 202010

With the following code, I'm able to extract year and month correctly if examdate is NOT missing

data want;
   set have;
newdate = examdate;
if newdate = . then newdate = 202009;
FORMAT newdate yymmn6.;
run;

if examdate is missing, I get the following

examdate newdate

.               251301

.               251301

 

Can anyone help so I get 202009 instead of 251301.

Thanks in advance,

Margaret 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You have to specify dates in a specific format in SAS or use the MDY() function. Both types of date require a day component though you don't need to use it since the format will not display it so just set it to the 1st or 15th as you prefer. 

 

To specify a date you need to use the DATE9 format.

 

if newdate = . then newdate = "01Sep2020"d;

or 

if newdate = . then newdate = mdy(9, 1, 2020);

SAS stores dates and times as numbers so when you provide 202009 that's a number that represents a date, SAS does not interpret it September 2020. 

 

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 


@urban58 wrote:

Hello SAS community,

I have the following   

examdate

08/25/2020

09/01/2020

.

.

09/14/2020

10/01/2020

 

 

I want to extract year and month, if examdate is missing then year and month variable (newdate)=202009 so I get the following

 

examdate newdate

08/25/2020 202008

09/01/2020 202009

.                  202009

.                  202009

09/14/2020 202009

10/01/2020 202010

With the following code, I'm able to extract year and month correctly if examdate is NOT missing

data want;
   set have;
newdate = examdate;
if newdate = . then newdate = 202009;
FORMAT newdate yymmn6.;
run;

if examdate is missing, I get the following

examdate newdate

.               251301

.               251301

 

Can anyone help so I get 202009 instead of 251301.

Thanks in advance,

Margaret 

 


 

View solution in original post

2 REPLIES 2
Reeza
Super User

You have to specify dates in a specific format in SAS or use the MDY() function. Both types of date require a day component though you don't need to use it since the format will not display it so just set it to the 1st or 15th as you prefer. 

 

To specify a date you need to use the DATE9 format.

 

if newdate = . then newdate = "01Sep2020"d;

or 

if newdate = . then newdate = mdy(9, 1, 2020);

SAS stores dates and times as numbers so when you provide 202009 that's a number that represents a date, SAS does not interpret it September 2020. 

 

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 


@urban58 wrote:

Hello SAS community,

I have the following   

examdate

08/25/2020

09/01/2020

.

.

09/14/2020

10/01/2020

 

 

I want to extract year and month, if examdate is missing then year and month variable (newdate)=202009 so I get the following

 

examdate newdate

08/25/2020 202008

09/01/2020 202009

.                  202009

.                  202009

09/14/2020 202009

10/01/2020 202010

With the following code, I'm able to extract year and month correctly if examdate is NOT missing

data want;
   set have;
newdate = examdate;
if newdate = . then newdate = 202009;
FORMAT newdate yymmn6.;
run;

if examdate is missing, I get the following

examdate newdate

.               251301

.               251301

 

Can anyone help so I get 202009 instead of 251301.

Thanks in advance,

Margaret 

 


 

urban58
Quartz | Level 8
Thank you Reeza, that worked perfectly!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 956 views
  • 0 likes
  • 2 in conversation