/* \gauss\src\seldata.src  written 4/16/97 based on Per's fsa2.src
  In turn I think this is based on Frank Windmeijer's code.

  This program includes three procedures detailed below.
  Basically given a Gauss data set with names 
  Calling opendat, rad, readdat in seldata.src gives
        x       - nobs*k matrix of explanatory variables
        y       - nobs*m matrix of dependent variables
        indy    - Index for the dependent variables
        indx    - Index for the exogenous variables
        f1      - File handle for the open data set
        n       - Number of observations in the data set
        nror    - number of times data set needs to be read to be complete
        rest    - number of obervations in the time data is read

************* Procedures ***************************************       Row
 Open the dataset and get the indices of the explanatory
 and dependent variables

{ f1,n,indy,indx } = opendat(data,yvar,xvar,&indices,m);                 ?

INPUT: 	data      - String data set to be used
    	yvar      - Character vecor of names of dependent variables
    	xvar      - Charcter vector of names of explantory variables
    	&indices  - Proc Indices.src, should be included in the main
                program
    	m         - The Number of dependent variables

OUTPUT: f1      - File handle for the open data set
    	n       - Number of observations in the data set
    	indy    - Index for the dependent variables
    	indx    - Index for the exogenous variables

-------------------------------------------------------------------
{ nror, rest } = rad(nobs,n);                                            ?

INPUT:  n       - number of observations in the data set
	nobs    - number of observations to read

OUTPUT: nror    - number of times data set needs to be read
                  to be complete
	y       - number of obervations in the time data is read

-----------------------------------------------------------------------

Reads the nobs observations of the data set

{ x,y } = Readdat(f1,r,nobs,indy,indx);                                197

INPUT:	f1      - File handle for the open data set
    	r       - the row where to start the reading
    	nobs    - number of observations to read
    	indy    - Index for the dependent variables
    	indx    - Index for the exogenous variables
	          program

OUTPUT: x       - nobs*k matrix of explanatory variables
    	y       - nobs*m matrix of dependent variables

-----------------------------------------------------------------------

*/

proc (4) = opendat(data,yvar,xvar,&index,m);
local f1,n,p,var;
local depvar,indy,ind,indx;
local index:proc;
p = rows(yvar|xvar);
  { var,ind } = index(data,yvar|xvar);
indy = ind[1:m];
indx = ind[m+1:p];
open f1 = ^data;
n = rowsf(f1);
retp(f1,n,indy,indx);
endp;

proc (2) = rad(nobs,n);
local a,nror,rest;
if n > nobs;
a = n/nobs;
nror = ceil(a);
rest = nobs*(a- (nror-1));
else;
nror = 1;
rest = n;
endif;
retp(nror,rest);
endp;

proc (2) = Readdat(f1,r,nobs,indy,indx);
local y,z,x;
y = seekr(f1,r);
    z = readr(f1,nobs);
    x = z[.,indx];
    y = z[.,indy];
retp(y,x);
endp;


