Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: generate variable versus define scalar, with conditional statement
From
annoporci <[email protected]>
To
[email protected]
Subject
Re: st: generate variable versus define scalar, with conditional statement
Date
Tue, 15 Jan 2013 21:47:59 +0800
you write faster than I think Nick!
Thank you very, very much.
scalar newscalar = open[1]
scalar newscalar = open[2]
would work fine.
I see. Thanks. That's actually the first thing I tried, but I need to be
able to specify the date in a "human readable" way, so I tried:
scalar newscalar = open[mdy(1,3,2012)]
It doesn't work. I'll use the method below then:
su open if date == mdy(1,3,2001), meanonly
scalar newscalar = r(min)
would also work.
But
scalar <name> = <varname>
will always be interpreted as
scalar <name> = <varname>[1]
Nick
On Tue, Jan 15, 2013 at 1:28 PM, Patrick Toche <[email protected]>
wrote:
Thanks for these explanations Nick!
if date == mdy(1,2,2001) scalar newscalar = open
still looks most unlikely to be what you really want _unless_ -date-
is also a scalar.
Barely had I pressed "send" that I realized something was not quite
right:
/// This one does what I intend
clear all
sysuse sp500
if date == mdy(1,2,2001) scalar newscalar = open
scalar list
/// This one doesn't do it:
clear all
sysuse sp500
if date == mdy(1,3,2001) scalar newscalar = open
scalar list
what's going on? The first command stores the value of open at the date
02jan2001. I expected the second one to store the value of open at the
date
03jan2001, but it doesn't ...
So I guess -date- is not a scalar or something ...
Oh wait, what I'm really asking is:
"I have an if or while command in my program that only seems to
evaluate
the first observation.What’s going on?"
http://www.stata.com/support/faqs/programming/if-command-versus-if-qualifier/
Okay, I need to follow that one up.
Thanks Nick!
Patrick
----
Patrick Toche.
On Tue, 15 Jan 2013 21:02:32 +0800, Nick Cox <[email protected]>
wrote:
What's intuitive is usually just what is familiar through experience
(although that is contentious within (?) 20% of the history of
psychology and 80% of the history of epistemology).
Stata has an -if- command and an -if- qualifier and one distinction
between them is in where they are used, not in what they look like at
first glance.
What comes first logically in either case?
If something is true, you want a scalar calculated.
You want a variable -generate-d using a specified rule but only -if-
something is true in each observation.
The trouble is that I think that way and explain it that way because I
have internalised Stata's syntax.
All that said,
1. I'd assert that the form you found puzzling is a common form in
many programming languages. It is the -if- qualifier that is
idiosyncratic. However, Stata learners are most likely to meet the
-if- qualifier first.
2.
if date == mdy(1,2,2001) scalar newscalar = open
still looks most unlikely to be what you really want _unless_ -date-
is also a scalar.
To see why, see
FAQ . . . . . . . . . . . . . . . . . . . . . if command vs. if
qualifier
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . J.
Wernow
6/00 I have an if command in my program that only seems
to evaluate the first observation, what's going on?
http://www.stata.com/support/faqs/programming/if-command-versus-if-qualifier/
Nick
On Tue, Jan 15, 2013 at 12:38 PM, annoporci <[email protected]>
wrote:
I learned something today I'd like to share with Statalist.
The way to conditionally define a variable and a scalar are
different. An
example is given below.
clear all
sysuse sp500
/// generate a new variable with missing entries except at the given
date
mdy(1,2,2001):
gen newvariable = open if date == mdy(1,2,2001)
/// define a new scalar by the value of variable open at the given
date
mdy(1,2,2001):
if date == mdy(1,2,2001) scalar newscalar = open
scalar list
/// THIS IS NOT THE WAY:
scalar newscalar = open if date == mdy(1,2,2001)
The above, incorrect way, is intuitive for a beginner (like myself),
it
is
similar to the syntax to generate new variables, but it won't work.
This is trivial for 99% of Statalist users, but may be a useful tip
for
the remaining 1%. And if not, almost surely for a future incarnation
of
myself scanning through Statalist for hints when the following error
message displays:
if not allowed
When I submitted *stata scalar "if not allowed"* Google returned a
solution,
in a more complicated setting:
http://statalist.1588530.n2.nabble.com/scalar-do-not-allow-if-td5748129.html
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/
--
Patrick Toche.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/