Warm tip: This article is reproduced from stackoverflow.com, please click
stata

Generating variable by groups taking values of certain observations

发布于 2020-05-03 18:16:15

I have a dataset with only variable values:

clear
input value new_var
1   1
3   3
5   5 
30  1 
40  3 
50  5 
11  1
12  3
13  5
end

How can I generate a new variable new_var containing a repeating sequence of the first three observations in value?

Questioner
garej
Viewed
24
Nick Cox 2020-02-16 20:27

Many ways to do it: here are two:

clear
input value new_var
1   1
3   3
5   5 
30  1 
40  3 
50  5 
11  1
12  3
13  5
end

egen index = seq(), to(3)
generate wanted = value[index]

generate direct = cond(mod(_n, 3) == 1, 1, cond(mod(_n, 3) == 2, 3, 5))

list, sep(3)

     +-------------------------------------------+
     | value   new_var   index   wanted   direct |
     |-------------------------------------------|
  1. |     1         1       1        1        1 |
  2. |     3         3       2        3        3 |
  3. |     5         5       3        5        5 |
     |-------------------------------------------|
  4. |    30         1       1        1        1 |
  5. |    40         3       2        3        3 |
  6. |    50         5       3        5        5 |
     |-------------------------------------------|
  7. |    11         1       1        1        1 |
  8. |    12         3       2        3        3 |
  9. |    13         5       3        5        5 |
     +-------------------------------------------+