<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Dynamic arithmetic expressions</title>
	<atom:link href="http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/</link>
	<description>computing for fun and profit</description>
	<lastBuildDate>Fri, 03 Feb 2012 03:42:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Gabi Huiber</title>
		<link>http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/comment-page-1/#comment-849</link>
		<dc:creator>Gabi Huiber</dc:creator>
		<pubDate>Fri, 08 May 2009 14:13:27 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=700#comment-849</guid>
		<description>I had no idea that rownonmiss() even existed. Thank you.</description>
		<content:encoded><![CDATA[<p>I had no idea that rownonmiss() even existed. Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric A. Booth</title>
		<link>http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/comment-page-1/#comment-825</link>
		<dc:creator>Eric A. Booth</dc:creator>
		<pubDate>Fri, 08 May 2009 02:18:57 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=700#comment-825</guid>
		<description>To add to the above comments, using the egen function -rownonmiss()- in place of your initial command:  

gen _var4=(_var1!=0)+(_var2!=0)+(_var3!=0)

may be better in some cases where you have missing values or string values in your observations.

EX:
your command above still counts missing values, if you wanted to run the same command but not count missing values you could run:

egen _var4 = rownonmiss(_var*) if _var1!=0 &#124; _var2!=0 &#124; _var3!=0

Another  approach is that the rononmiss() option can count the number of non-zero, non-missing observations in a row, even if there are string values present by using the &quot;strok&quot; option, e.g., 

egen _var4 = rownonmiss(_var*) if _var1!=0 &#124; _var2!=0 &#124; _var3!=0, strok</description>
		<content:encoded><![CDATA[<p>To add to the above comments, using the egen function -rownonmiss()- in place of your initial command:  </p>
<p>gen _var4=(_var1!=0)+(_var2!=0)+(_var3!=0)</p>
<p>may be better in some cases where you have missing values or string values in your observations.</p>
<p>EX:<br />
your command above still counts missing values, if you wanted to run the same command but not count missing values you could run:</p>
<p>egen _var4 = rownonmiss(_var*) if _var1!=0 | _var2!=0 | _var3!=0</p>
<p>Another  approach is that the rononmiss() option can count the number of non-zero, non-missing observations in a row, even if there are string values present by using the "strok" option, e.g., </p>
<p>egen _var4 = rownonmiss(_var*) if _var1!=0 | _var2!=0 | _var3!=0, strok</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabi Huiber</title>
		<link>http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/comment-page-1/#comment-800</link>
		<dc:creator>Gabi Huiber</dc:creator>
		<pubDate>Wed, 06 May 2009 03:37:51 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=700#comment-800</guid>
		<description>I figured that the equal sign in the definition of local var_sum was going to bite if the full expression turned out to be longer than 244 characters. And of course you can run through that many characters pretty quickly if your logical operators are compounded -- as in your case, where you want both non-zero and non-missing instances. Nice hack, that if-else workaround.</description>
		<content:encoded><![CDATA[<p>I figured that the equal sign in the definition of local var_sum was going to bite if the full expression turned out to be longer than 244 characters. And of course you can run through that many characters pretty quickly if your logical operators are compounded -- as in your case, where you want both non-zero and non-missing instances. Nice hack, that if-else workaround.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/comment-page-1/#comment-797</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Tue, 05 May 2009 21:06:56 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=700#comment-797</guid>
		<description>Gabi,

That unab vars : *var* is another good trick. Anytime STATA syntax accepts a &quot;varlist&quot; you should be able to use the different wildcard or shorthand operators listed under the varlist helpfile. 

Another quick thing that I ran into when implementing this code was that my vars_sum macro was getting cut off by running it through the substr to get rid of that last plus sign. As you probably know, STATA&#039;s max string length is 244 characters. So if you have long variable names or many variables to check for non-zero values, your formula might hit the max character length for strings. Since the maximum characters allowed in a macro is much larger, I kept my formula in the macro domain like so:

unab vars : _var1-_var39;
local vars_count : word count `vars&#039;;
local z = 1;
foreach x of local vars {;
	if `z&#039; == `vars_count&#039; {;
		local vars_sum &quot;`vars_sum&#039; (`x&#039; != 0 &amp; `x&#039; &lt; .)&quot;;
		continue;
	};
	else {;
		local vars_sum &quot;`vars_sum&#039; (`x&#039; != 0 &amp; `x&#039; &lt; .) +&quot;;
	};
	local z = `z&#039; + 1;
	};
gen _var_count = `vars_sum&#039;;

My code is delimited with a &#039;;&#039;, and I needed to count all non-zero non-missing values, hence the addition of the `x&#039;&lt;. statement to your formula.</description>
		<content:encoded><![CDATA[<p>Gabi,</p>
<p>That unab vars : *var* is another good trick. Anytime STATA syntax accepts a "varlist" you should be able to use the different wildcard or shorthand operators listed under the varlist helpfile. </p>
<p>Another quick thing that I ran into when implementing this code was that my vars_sum macro was getting cut off by running it through the substr to get rid of that last plus sign. As you probably know, STATA's max string length is 244 characters. So if you have long variable names or many variables to check for non-zero values, your formula might hit the max character length for strings. Since the maximum characters allowed in a macro is much larger, I kept my formula in the macro domain like so:</p>
<p>unab vars : _var1-_var39;<br />
local vars_count : word count `vars';<br />
local z = 1;<br />
foreach x of local vars {;<br />
	if `z' == `vars_count' {;<br />
		local vars_sum "`vars_sum' (`x' != 0 &amp; `x' &lt; .)";<br />
		continue;<br />
	};<br />
	else {;<br />
		local vars_sum "`vars_sum' (`x' != 0 &amp; `x' &lt; .) +";<br />
	};<br />
	local z = `z' + 1;<br />
	};<br />
gen _var_count = `vars_sum';</p>
<p>My code is delimited with a ';', and I needed to count all non-zero non-missing values, hence the addition of the `x'&lt;. statement to your formula.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabi Huiber</title>
		<link>http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/comment-page-1/#comment-796</link>
		<dc:creator>Gabi Huiber</dc:creator>
		<pubDate>Tue, 05 May 2009 19:39:27 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=700#comment-796</guid>
		<description>Hi Nick. You&#039;re right. Your note sent me digging for a good reason why I built the _vars list the way I did, and I couldn&#039;t find one. The names of the actual variables of interest in the data set are different from simply _var, of course, but they do share a stub, enough for what you&#039;re proposing to work fine. 

As it turns out, &lt;code&gt;unab&lt;/code&gt; is quite flexible in its pattern recognition capabilities. For example, &lt;code&gt;unab vars: *var*&lt;/code&gt; would pick out all the variable names that contained the string &quot;var&quot; anywhere. So, sorry about the code bloat.

Thank you for visiting. I&#039;m glad you like the place.</description>
		<content:encoded><![CDATA[<p>Hi Nick. You're right. Your note sent me digging for a good reason why I built the _vars list the way I did, and I couldn't find one. The names of the actual variables of interest in the data set are different from simply _var, of course, but they do share a stub, enough for what you're proposing to work fine. </p>
<p>As it turns out, <code>unab</code> is quite flexible in its pattern recognition capabilities. For example, <code>unab vars: *var*</code> would pick out all the variable names that contained the string "var" anywhere. So, sorry about the code bloat.</p>
<p>Thank you for visiting. I'm glad you like the place.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/comment-page-1/#comment-795</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Tue, 05 May 2009 19:10:02 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=700#comment-795</guid>
		<description>I enjoy the blog and this is definitely a helpful post. 

To get all the variables that start with &quot;_var&quot;, couldn&#039;t you just write: 

unab vars : _vars*

I use that all the time when working with numerically named variables with a common stub.</description>
		<content:encoded><![CDATA[<p>I enjoy the blog and this is definitely a helpful post. </p>
<p>To get all the variables that start with "_var", couldn't you just write: </p>
<p>unab vars : _vars*</p>
<p>I use that all the time when working with numerically named variables with a common stub.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabi Huiber</title>
		<link>http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/comment-page-1/#comment-747</link>
		<dc:creator>Gabi Huiber</dc:creator>
		<pubDate>Wed, 29 Apr 2009 14:12:07 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=700#comment-747</guid>
		<description>Reshape is one of my all-time favorite Stata commands. Nice having you here, Keith.</description>
		<content:encoded><![CDATA[<p>Reshape is one of my all-time favorite Stata commands. Nice having you here, Keith.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith</title>
		<link>http://enoriver.net/index.php/2009/04/27/dynamic-arithmetic-expressions/comment-page-1/#comment-745</link>
		<dc:creator>Keith</dc:creator>
		<pubDate>Wed, 29 Apr 2009 13:47:21 +0000</pubDate>
		<guid isPermaLink="false">http://enoriver.net/?p=700#comment-745</guid>
		<description>This will do it with less code:

reshape long _var, i(id) j(varnum)
gen _varnonzero = (_var!=0)   // are you missing  &quot;&amp; (_var!=.)&quot; ?
bysort id: egen count = total( _varnonzero)
drop _varnonzero
reshape wide _var, i(id) j(varnum)
rename count vars_sum</description>
		<content:encoded><![CDATA[<p>This will do it with less code:</p>
<p>reshape long _var, i(id) j(varnum)<br />
gen _varnonzero = (_var!=0)   // are you missing  "&amp; (_var!=.)" ?<br />
bysort id: egen count = total( _varnonzero)<br />
drop _varnonzero<br />
reshape wide _var, i(id) j(varnum)<br />
rename count vars_sum</p>
]]></content:encoded>
	</item>
</channel>
</rss>

