' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'	SUBROUTINE:	x13force.prg
'
'	PURPOSE: 		A subroutine for x13 seasonal adjustment that enforces sum of adjusted series to equal sum of original series. 
'
'	USAGE:		 		call x13force(series series_name)
'
' 	DATE:				19/11/2015
'
'	AUTHOR:			Graeme Walsh
' ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

subroutine x13force(series series_name)
	
	' Create the x13 specification file
	  text seas_adj_spec
	  seas_adj_spec.append "transform{"
	  seas_adj_spec.append "function = auto"
	  seas_adj_spec.append "}"
	  seas_adj_spec.append "pickmdl{"
	  seas_adj_spec.append "mode=both"
	  seas_adj_spec.append "}"
	  seas_adj_spec.append "x11{"
	  seas_adj_spec.append "save = (d11)"
	  seas_adj_spec.append "}"
	
	' Below is the important bit
	  seas_adj_spec.append "force{"
	  seas_adj_spec.append "type=regress"
	  seas_adj_spec.append "save = saa"
	  seas_adj_spec.append "}"	
	  
	' Standard x13 EViews function call (above specification file used as input)
	  series_name.x13( spec = seas_adj_spec ) 

	' Same as above except spec file and log file are also returned	  
	' series_name.x13( spec = seas_adj_spec, savespec = seas_adj_spec2, errlog = seas_adj_spec_log)
	
	' Define variables
	  %file_name 	= @temppath+"\EVX13TMP.saa"
	  %varname 	= series_name.@name+"_saa"
	  %format 		= "year 1-4 period 5-6 "+%varname+" 7-50" 
	
	' Import the temporary file with suffix .saa (this is the adjusted series where sums of adjusted series equal sums of original series)
	  if @pagefreq = "M" then
	  	  import {%file_name} ftype=ascii rectype=crlf rformat=%format colhead=2 descriptions=("","","") eoltype=pad badfield=NA @freq M @id @date(year,period) @destid @date @smpl @all	
	  else 
	  if @pagefreq = "Q" then
		  import {%file_name} ftype=ascii rectype=crlf rformat=%format colhead=2 descriptions=("","","") eoltype=pad badfield=NA @freq Q @id @date(year,period) @destid @date @smpl @all
	  endif			
	  endif
	
	' Tidy up
	  delete seas_adj_spec
	
endsub
