Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: generate variable versus define scalar, with conditional statement

From   annoporci <>
To   "" <>
Subject   st: generate variable versus define scalar, with conditional statement
Date   Tue, 15 Jan 2013 20:38:13 +0800

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
gen newvariable = open if date == mdy(1,2,2001)

/// define a new scalar by the value of variable open at the given date
if date == mdy(1,2,2001) scalar newscalar = open
scalar list

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:

Patrick Toche.
*   For searches and help try:

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