Rajshri Suresh
Rajshri Suresh

Reputation: 23

Creating a new variable that counts number of variables whose values fall within a range

I am trying to create a new variable that counts the number of temperature variables whose value falls within a certain range of the temperature.

For example, my data looks like this -

Data sample

I would like to create a new variable that counts the number of days where the temperature was within 30°C - 31.9°C. I tried to use egen - anycount() in Stata but it won't accept non-integers in the numlist.

Upvotes: 0

Views: 1601

Answers (1)

Arthur Morris
Arthur Morris

Reputation: 1348

You can loop over the variables:

local vars T* // since your variables are named with this convention

generate count = 0
foreach v of local vars {
    replace count = count + 1 if inrange(`v', 30, 31.9)
}

Upvotes: 2

Related Questions