BookmarkSubscribeRSS Feed
Calcite | Level 5

The following code works for 3 of my 4 variables. But with one variable, I get the following message:



ERROR: (execution) Invalid argument to function.


 operation : SQRT at line 4121 column 1

 operands  : _TEM1001


_TEM1001      5 rows      1 col     (numeric)








 statement : ASSIGN at line 4121 column 1

ERROR: (execution) Matrix has not been set to a value.


Here is the macro code: 


%MACRO clus2OLS(yvar, xvars, cluster1, cluster2, dset);
	/* do interesection cluster*/
	proc surveyreg data=&dset; cluster &cluster1 &cluster2; model &yvar= &xvars /  covb; ods output CovB = CovI; quit;
	/* Do first cluster */
	proc surveyreg data=&dset; cluster &cluster1; model &yvar= &xvars /  covb; ods output CovB = Cov1; quit;
	/* Do second cluster */
	proc surveyreg data=&dset; cluster &cluster2; model &yvar= &xvars /  covb; ods output CovB = Cov2 ParameterEstimates = params;	quit;

	/*	Now get the covariances numbers created above. Calc coefs, SEs, t-stats, p-vals	using COV = COV1 + COV2 - COVI*/
	proc iml; reset noprint; use params;
		read all var{Parameter} into varnames;
		read all var _all_ into b;
		use Cov1; read all var _num_ into x1;
	 	use Cov2; read all var _num_ into x2;
	 	use CovI; read all var _num_ into x3;

		cov = x1 + x2 - x3;	/* Calculate covariance matrix */
		dfe = b[1,3]; stdb = sqrt(vecdiag(cov)); beta = b[,1]; t = beta/stdb; prob = 1-probf(t#t,1,dfe); /* Calc stats */

		print,"Parameter estimates",,varnames beta[format=8.4] stdb[format=8.4] t[format=8.4] prob[format=8.4];

		  conc =    beta || stdb || t || prob;
  		  cname = {"estimates" "stderror" "tstat" "pvalue"};
  		  create clus2dstats from conc [ colname=cname ];
          append from conc;

		  conc =   varnames;
  		  cname = {"varnames"};
  		  create names from conc [ colname=cname ];
          append from conc;

	data clus2dstats; merge names clus2dstats; run;
%MEND clus2OLS;

%clus2OLS(yvar=dependentvariable, xvars=listofindependentvariables, cluster1=gvkey, cluster2=fyear, dset=gtx.prepforreg);


Here is how I call the macro


%clus2OLs(yvar=Chgroa3, xvars=vb_nvb roa chgroa GrAS, cluster1=gvkey, cluster2=fyear, dset=Reg_ROA);


I receive PARAMETER ESTIMATES when I use a different "yvar" from the same dataset. But when I use this similar variable in my call statement (Chgroa3), I get the above error messages and no PARAMETER ESTIMATES. 


Thank you



Your covariance matrix is wrong. Consequently you are getting an error (the sqrt of a negative number) when you compute

cov = x1 + x2 - x3;

stdb = sqrt(vecdiag(cov));


There is no reason why a linear combination of matrices should be a valid covariance matrix and clearly it is not for your data.

Diamond | Level 26

ERROR: (execution) Invalid argument to function.


 operation : SQRT at line 4121 column 1


You can't take a square root of a negative number. You get a negative number from 


cov = x1 + x2 - x3;

I don't think this is really a way to obtain a covariance matrix, and I have never seen anyone subtract a covariance from another covariance matrix, so I'm not really sure why you are doing this subtraction.

Paige Miller



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 3 in conversation