BookmarkSubscribeRSS Feed
emrancaan
Obsidian | Level 7

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_PeriodlagperiodlagperiodBeglagperiodEndperiodperiodBegperiodEnd
Monthly02-20182018-02-012018-02-2803-20182018-03-012018-03-31
Monthly01-20182018-01-012018-01-3102-20182018-02-012018-02-28

 

Or more precisely

 

Rep_PeriodlagperiodlagperiodBeglagperiodEndperiodperiodBegperiodEnd
Monthly02-20182018-02-012018-02-2803-20182018-03-012018-03-31
Monthly01-20182018-01-012018-01-3102-20182018-02-012018-02-28
       
Monthly05-20172017-05-012017-05-3106-20172017-06-012017-06-30
Quarterly03-20172017-01-012017-03-3105-20172017-05-012017-05-31
Quarterly12-20162016-10-012016-12-3103-20172017-01-012017-03-31
       
7 REPLIES 7
emrancaan
Obsidian | Level 7
I want to genarate three columns 2,3,4 having lag periods
ChrisNZ
Tourmaline | Level 20

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
emrancaan
Obsidian | Level 7

Thanks Chris, final dataset seems fine but I also need LagperiodBeg and LagperiodEnd. I used lag() but it didn't work.

ChrisNZ
Tourmaline | Level 20
You can't use lag() in sql. Just use the same logic as for the other fields .
emrancaan
Obsidian | Level 7

I cannot generate he required output fully It is failing at

 

Quarterly03-20172017-01-012017-03-3105-20172017-05-012017-05-31
Astounding
PROC Star

To get "prior" period dates, use INTNX.  Just change the 0 to -1 to go back one period.

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 2122 views
  • 1 like
  • 3 in conversation