Bookmark and Share

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   "Seed, Paul" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: generate variable versus define scalar, with conditional statement
Date   Wed, 16 Jan 2013 11:44:55 +0000

Dear Statalist, 

It appears that what Patrick Toche wants is not something that 
Stata can do in a single command.  This is because what he wants 
is not completely clear.

He wants for the system dataset -sp500- to produce a scalar that gives 
the value of a particular variable (-open-) at a particular date 
chosen by him (-date-).

Depending on the data set , this is an ambiguous request.  Suppose the date 
in question occurs more than once, with different values of -open- attached.
[It doesn't in this case, but it could easily do].
Does he want the minimum, the maximum, the average, or a missing value?

The following covers all these options.

	clear all
	sysuse sp500

	summ  open if date == mdy(1,2,2001), meanonly
/// the meanonly option suppresses output, and saves computer time 
///by not calculating the standard deviation.  It is not essential.
	scalar newscalar_mean = r(mean)  
	scalar newscalar_min= r(min)  
	scalar newscalar_max= r(max)  
	if r(n) == 1 scalar newscalar = r(mean)  

Or, he can check if each date occurs at most once, 
and simplify his code.
	bys date : assert _n == 1
	summ  open if date == mdy(1,2,2001), meanonly
	scalar newscalar = r(mean)  

BW

Paul T Seed, Senior Lecturer in Medical Statistics, 
Division of Women's Health, King's College London
Women's Health Academic Centre, King's Health Partners 
(+44) (0) 20 7188 3642.



*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index