Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]


From   "Nick Winter" <>
To   <>
Subject   RE: st: STATA Vs. SAS
Date   Wed, 27 Nov 2002 16:04:48 -0500

> -----Original Message-----
> From: Richard Herrell [] 
> Sent: Wednesday, November 27, 2002 3:56 PM
> To:
> Subject: RE: st: STATA Vs. SAS
> On Wed, 27 Nov 2002, Nick Cox wrote:
> > I am still not clear whether -x- and -y- are variables, 
> Yup, x and y are variables with values for each observation 
> in the data
> set.
> > and whether this all -- in Some Alternative
> > Software -- is tacitly looping over 
> > observations. (In my case, please assume 
> > complete ignorance of SAS.) 
> In SAS (and dBase or SPSS, e.g.), these commands are 
> evaluated for each
> observation.  In such a structure, once a true condition is met,
> subsequent conditions are skipped.  Say I want to assign grades to my
> students' percentage scores.  In SAS I could write
> if      score > 90 then grade='A';
> else if score > 80 then grade='B';
> else if score > 70 then grade='C';
> else if score > 60 then grade='D';
> SAS will assign the appropriate grade to the first condition 
> that is true,
> then skip the following conditions in the structure.  (This 
> is also true
> of other database management languages I've used such as 
> dBase.)  This is
> why I find it more efficient and less cumbersome than writing 
> the complete
> condition for a series of "if ... then"s when every one will 
> be evaluated.

Much of this probably boils down to what you learned first, and
therefore what seems "natural".  Leaving efficiency aside for a moment,
though, it does seem worth pointing out that in Stata you need not have
syntax any more complex than the syntax listed above:

	generate grade="D" if score > 60
	replace grade="C" if score > 70
	replace grade="B" if score > 80
	replace grade="A" if score > 90

That is, in this case, by reversing the order, you can avoid saying
things like

	replace grade="B" if score>80 & score<=90

and so on.

Hugely complex nested conditions might be easier in SAS.  It could be
argued that

	if cond1
		if condA
			then x=1
		else if condB
			then x=2
		else if condC
			then x=3
	else if cond2
		if condA
			then x=4
		if condB
			then x=5

displays the structure more clearly than

	gen x=1 if cond1 & condA
	replace x=2 if cond1 & condB
	replace x=3 if cond1 & condC
	replace x=4 if cond2 & condD

The downside is that if you don't indent carefully, and/or if you miss
an else, or whatever, then it might not be transparent exactly what set
of conditions hold at a given point in the SAS massive if/then/else

--Nick Winter
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index