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