I'm just wondering how to calculate the lag dates for previous month and previous quarter in Proc SQL. I'm using following proc sql but I cannot generate the lag period for each period.
proc sql; CREATE TABLE periods_eba as Select distinct CASE WHEN calendar_day > &qtrly_cutoff_date THEN 'Monthly' ELSE 'Quarterly' END AS Rep_Period, intnx('month',calendar_day,0,'beginning') as period format=mmyyd7., CASE WHEN calendar_day > &qtrly_cutoff_date THEN intnx('month',calendar_day,0,'beginning') ELSE intnx('qtr',calendar_day,0,'beginning') END as periodBeg format=yymmdd10., intnx('month',calendar_day,0,'end') as periodEnd format=yymmdd10. from ebaperiod Order by lagperiod desc ;QUIT;
My intention is to create
1) From Today till May 2017 monthly periods
2) From May 2017 to Jan2010 Quarterly periods
final data set will contain lag periods ..and it will look like similar to below.
Rep_Period | lagperiod | lagperiodBeg | lagperiodEnd | period | periodBeg | periodEnd |
Monthly | 02-2018 | 2018-02-01 | 2018-02-28 | 03-2018 | 2018-03-01 | 2018-03-31 |
Monthly | 01-2018 | 2018-01-01 | 2018-01-31 | 02-2018 | 2018-02-01 | 2018-02-28 |
Or more precisely
Rep_Period | lagperiod | lagperiodBeg | lagperiodEnd | period | periodBeg | periodEnd |
Monthly | 02-2018 | 2018-02-01 | 2018-02-28 | 03-2018 | 2018-03-01 | 2018-03-31 |
Monthly | 01-2018 | 2018-01-01 | 2018-01-31 | 02-2018 | 2018-02-01 | 2018-02-28 |
Monthly | 05-2017 | 2017-05-01 | 2017-05-31 | 06-2017 | 2017-06-01 | 2017-06-30 |
Quarterly | 03-2017 | 2017-01-01 | 2017-03-31 | 05-2017 | 2017-05-01 | 2017-05-31 |
Quarterly | 12-2016 | 2016-10-01 | 2016-12-31 | 03-2017 | 2017-01-01 | 2017-03-31 |
Your SQL looks fine, What is the issue?
I am unsure why you can't do it. Like this?
proc sql;
create table PERIODS_EBA as
select distinct
ifc(CALENDAR_DAY > &qtrly_cutoff_date., 'Monthly', 'Quarterly') as REP_PERIOD length=9
,intnx('month',calendar_day,0 ,'beginning') as PERIOD format=mmyyd7.
,intnx('month',calendar_day,-1,'beginning') as LAGPERIOD format=mmyyd7.
,intnx(ifc(CALENDAR_DAY > &qtrly_cutoff_date.,'month','qtr')
,CALENDAR_DAY,0,'beginning') as PERIODBEG format=yymmdd10.
,intnx('month',CALENDAR_DAY,0,'end') as PERIODEND format=yymmdd10.
from EBAPERIOD
;
quit;
REP_PERIOD | PERIOD | LAGPERIOD | PERIODBEG | PERIODEND |
---|---|---|---|---|
Monthly | 01-2018 | 12-2017 | 2018-01-01 | 2018-01-31 |
Thanks Chris, final dataset seems fine but I also need LagperiodBeg and LagperiodEnd. I used lag() but it didn't work.
I cannot generate he required output fully It is failing at
Quarterly | 03-2017 | 2017-01-01 | 2017-03-31 | 05-2017 | 2017-05-01 | 2017-05-31 |
To get "prior" period dates, use INTNX. Just change the 0 to -1 to go back one period.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.