@wisej wrote:
Hi,
I have a date variable formatted as yyddmm (ex. 2021-04-06). I need to run descriptive statistics on data collected last month. Any advice on how to do this if the date variable is formatted this way? I appreciate any help!
The word FORMAT has a special meaning in SAS. A format is instructions for how to display values as text. SAS does not have any date format that would display dates in YDM order. It does have some, like YYMMDD, that will display dates in YMD order.
For purposes of providing an answer lets assume that there is a variable named DATE that contains date values (number of days since 1960). Most likely if it display as string like '2021-04-06' then it has the format specification YYMMDD10. attached to it (although there are other date specifications that would display dates in that same style).
To test for last month you need to know today's month. You can use the DATE() function (or its alias name of TODAY()) to get today's date. Then you can move back one month to find last month by using the INTNX() function with the interval type of month and and offset of negative one.
intnx('month',today(),-1)
then you can test if the value is in last month in a number of ways.
You could convert DATE to the same date and compare:
where intnx('month',DATE,-1) = intnx('month',today(),-1) ;
You could convert both the strings using a format that only displays the YEAR and MONTH part of the date and compare those strings.
where put(DATE,monyy7.) = put(intnx('month',today(),-1),monyy7.) ;
Or you could test if the date is in the interval defined by the first and last day of the month. That might work faster if the source data is indexed on DATE.
where date between intnx('month',today(),-1,'b') and intnx('month',today(),-1,'e');