Dear all,
I am using a dataset that is such that for a given value of the identifying variable ("var_id") we observe several values for another variable, let's say "var_of_interest".
In particular, the dataset is like
I would like to create a variable ("new_var") that counts the number of distinct values of "var_of_interest" that are different from "BA".
In particular, I would like the dataset to be like the following
where "new_var" is the variable that retrieves the quantity of interest for each different id (from the variable "var_id").
Can you help me with that?
Thank you very much.
Any help is greatly appreciated.
Obs: here is the code to import the fictitious data:
clear
input var_id str2 var_of_interest
11 "PI"
11 "PA"
11 "CE"
11 "BA"
12 "SP"
12 "SP"
12 "BA"
12 "RN"
end
I am using a dataset that is such that for a given value of the identifying variable ("var_id") we observe several values for another variable, let's say "var_of_interest".
In particular, the dataset is like
var_id | var_of_interest |
11 | PI |
11 | PA |
11 | CE |
11 | BA |
12 | SP |
12 | SP |
12 | BA |
12 | RN |
I would like to create a variable ("new_var") that counts the number of distinct values of "var_of_interest" that are different from "BA".
In particular, I would like the dataset to be like the following
var_id | var_of_interest | new_var |
11 | PI | 3 |
11 | PA | 3 |
11 | CE | 3 |
11 | BA | 3 |
12 | SP | 2 |
12 | SP | 2 |
12 | BA | 2 |
12 | RN | 2 |
where "new_var" is the variable that retrieves the quantity of interest for each different id (from the variable "var_id").
Can you help me with that?
Thank you very much.
Any help is greatly appreciated.
Obs: here is the code to import the fictitious data:
clear
input var_id str2 var_of_interest
11 "PI"
11 "PA"
11 "CE"
11 "BA"
12 "SP"
12 "SP"
12 "BA"
12 "RN"
end
Comment