<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Macro: OLS with two-way cluster in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Macro-OLS-with-two-way-cluster/m-p/486782#M4305</link>
    <description>&lt;P&gt;The following code works for 3 of my 4 variables. But with one variable, I get the following message:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: IML Ready&lt;/P&gt;&lt;P&gt;ERROR: (execution) Invalid argument to function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;operation : SQRT at line 4121 column 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;operands&amp;nbsp; : _TEM1001&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;_TEM1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5 rows&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1 col&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (numeric)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;0.0002809&lt;/P&gt;&lt;P&gt;&amp;nbsp;0.0005076&lt;/P&gt;&lt;P&gt;&amp;nbsp;0.0112643&lt;/P&gt;&lt;P&gt;&amp;nbsp; -0.00117&lt;/P&gt;&lt;P&gt;&amp;nbsp;0.0018209&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;statement : ASSIGN at line 4121 column 1&lt;/P&gt;&lt;P&gt;ERROR: (execution) Matrix has not been set to a value.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the macro code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO clus2OLS(yvar, xvars, cluster1, cluster2, dset);
	/* do interesection cluster*/
	proc surveyreg data=&amp;amp;dset; cluster &amp;amp;cluster1 &amp;amp;cluster2; model &amp;amp;yvar= &amp;amp;xvars /  covb; ods output CovB = CovI; quit;
	/* Do first cluster */
	proc surveyreg data=&amp;amp;dset; cluster &amp;amp;cluster1; model &amp;amp;yvar= &amp;amp;xvars /  covb; ods output CovB = Cov1; quit;
	/* Do second cluster */
	proc surveyreg data=&amp;amp;dset; cluster &amp;amp;cluster2; model &amp;amp;yvar= &amp;amp;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;
	quit;

	data clus2dstats; merge names clus2dstats; run;
%MEND clus2OLS;

%clus2OLS(yvar=dependentvariable, xvars=listofindependentvariables, cluster1=gvkey, cluster2=fyear, dset=gtx.prepforreg);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is how I call the macro&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%clus2OLs(yvar=Chgroa3, xvars=vb_nvb roa chgroa GrAS, cluster1=gvkey, cluster2=fyear, dset=Reg_ROA);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Aug 2018 19:22:34 GMT</pubDate>
    <dc:creator>manleygurl</dc:creator>
    <dc:date>2018-08-14T19:22:34Z</dc:date>
    <item>
      <title>Macro: OLS with two-way cluster</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Macro-OLS-with-two-way-cluster/m-p/486782#M4305</link>
      <description>&lt;P&gt;The following code works for 3 of my 4 variables. But with one variable, I get the following message:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: IML Ready&lt;/P&gt;&lt;P&gt;ERROR: (execution) Invalid argument to function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;operation : SQRT at line 4121 column 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;operands&amp;nbsp; : _TEM1001&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;_TEM1001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5 rows&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1 col&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (numeric)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;0.0002809&lt;/P&gt;&lt;P&gt;&amp;nbsp;0.0005076&lt;/P&gt;&lt;P&gt;&amp;nbsp;0.0112643&lt;/P&gt;&lt;P&gt;&amp;nbsp; -0.00117&lt;/P&gt;&lt;P&gt;&amp;nbsp;0.0018209&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;statement : ASSIGN at line 4121 column 1&lt;/P&gt;&lt;P&gt;ERROR: (execution) Matrix has not been set to a value.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the macro code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO clus2OLS(yvar, xvars, cluster1, cluster2, dset);
	/* do interesection cluster*/
	proc surveyreg data=&amp;amp;dset; cluster &amp;amp;cluster1 &amp;amp;cluster2; model &amp;amp;yvar= &amp;amp;xvars /  covb; ods output CovB = CovI; quit;
	/* Do first cluster */
	proc surveyreg data=&amp;amp;dset; cluster &amp;amp;cluster1; model &amp;amp;yvar= &amp;amp;xvars /  covb; ods output CovB = Cov1; quit;
	/* Do second cluster */
	proc surveyreg data=&amp;amp;dset; cluster &amp;amp;cluster2; model &amp;amp;yvar= &amp;amp;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;
	quit;

	data clus2dstats; merge names clus2dstats; run;
%MEND clus2OLS;

%clus2OLS(yvar=dependentvariable, xvars=listofindependentvariables, cluster1=gvkey, cluster2=fyear, dset=gtx.prepforreg);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is how I call the macro&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%clus2OLs(yvar=Chgroa3, xvars=vb_nvb roa chgroa GrAS, cluster1=gvkey, cluster2=fyear, dset=Reg_ROA);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 19:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Macro-OLS-with-two-way-cluster/m-p/486782#M4305</guid>
      <dc:creator>manleygurl</dc:creator>
      <dc:date>2018-08-14T19:22:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro: OLS with two-way cluster</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Macro-OLS-with-two-way-cluster/m-p/486785#M4306</link>
      <description>&lt;P&gt;Your covariance matrix is wrong. Consequently you are getting an error (the sqrt of a negative number) when you compute&lt;/P&gt;
&lt;P&gt;cov = x1 + x2 - x3;&lt;/P&gt;
&lt;P&gt;stdb = sqrt(vecdiag(cov));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no reason why a linear combination&amp;nbsp;of matrices should be a valid covariance matrix and clearly it is not for your data.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 19:27:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Macro-OLS-with-two-way-cluster/m-p/486785#M4306</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-08-14T19:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro: OLS with two-way cluster</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Macro-OLS-with-two-way-cluster/m-p/486786#M4307</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;ERROR: (execution) Invalid argument to function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;operation : SQRT at line 4121 column 1&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can't take a square root of a negative number. You get a negative number from&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;cov &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; x1 &lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt; x2 &lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt; x3&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 19:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Macro-OLS-with-two-way-cluster/m-p/486786#M4307</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-08-14T19:27:50Z</dc:date>
    </item>
  </channel>
</rss>

