Rhodochrosite | Level 12

## How to require the "AUG2022" date?

Hello,

I would like to get the month of "AUG2022" in the variable "scrdate_n."  The variable format is MONYY7.   Please guild me how; thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: How to require the "AUG2022" date?

@ybz12003 wrote:
Spoiler
``if scrdate_n in ('AUG2022'd);``

The log window shows an error message, ERROR: Invalid date/time/datetime constant 'AUG2022'd.

Several ways to check if a date occurs in a given month:

```If month(scrdate_n)=8 and year(scrdate_n)=2022 then ....

if put(scrdate_n,monyy7.)='AUG2022' then ...

if intnx('month',scrdate_n,0,'B') = '01AUG2022'd then ...```

for 3 examples.

6 REPLIES 6
Rhodochrosite | Level 12

## Re: How to require the "AUG2022" date?

Spoiler
``if scrdate_n in ('AUG2022'd);``

The log window shows an error message, ERROR: Invalid date/time/datetime constant 'AUG2022'd.

Super User

## Re: How to require the "AUG2022" date?

You cannot have a date without a day of the month.

You could pick any day of the month, but normally people just use the first of the month.

``if scrdate_n in ('01AUG2022'd);``

But if you really want to test if SCRDATE_N is ANY day in that month then you probably need to work harder.

``````if put(scrdate_n,monyy7.)  in ('AUG2022');
if intnx('month',scrdate_n,0,'b') in ('01AUG2022'd);``````
Rhodochrosite | Level 12

## Re: How to require the "AUG2022" date?

I know the day I put was wrong. how to correct it? I only need the month.
Super User

## Re: How to require the "AUG2022" date?

If you want to specify a date value you HAVE to have a day of the month.

A number can only have one value.  It cannot have a range of values.

Super User

## Re: How to require the "AUG2022" date?

@ybz12003 wrote:
Spoiler
``if scrdate_n in ('AUG2022'd);``

The log window shows an error message, ERROR: Invalid date/time/datetime constant 'AUG2022'd.

Several ways to check if a date occurs in a given month:

```If month(scrdate_n)=8 and year(scrdate_n)=2022 then ....

if put(scrdate_n,monyy7.)='AUG2022' then ...

if intnx('month',scrdate_n,0,'B') = '01AUG2022'd then ...```

for 3 examples.

SAS Super FREQ

## Re: How to require the "AUG2022" date?

I recommend you check out the SAS Date, Time, and Datetime Values  documentation

Here's an example

``````data work.have ;
format date monyy. ;
do date="25jul2023"d to "10Sep2023"d by 7 ;
output work.have ;
end ;
run ;

data work.want ;
set work.have ;
put "Have : " date= date= date7. date= 8. ;
if "01Aug2023"d <= date < "01Sep2023"d then do ;
put "        Want : " date= date= date7. date= 8. ;
output work.want ;
end ;
else
put "  Don't Want : " date= date= date7. date= 8. ;

run ;``````
Discussion stats
• 6 replies
• 994 views
• 3 likes
• 4 in conversation