I have 100 data sets, they have a different number of variables. For example, some data sets have 33 variables (pre_tos1-pre_tos33), but others have a smaller number of variables than them (e.g., 29 variables, or 31 variables). Here, I want to create variables if they do not exist in the data set, and assign them to zero.
I tried this work using the following codes.
variable pre_tos7 not found
r(111);
For example, the first data set does not have variable pre_tos7. When I used the following codes, the following error appears:
Could you please provide any advice?
I tried this work using the following codes.
HTML Code:
quietly forvalues s=1/33 { gen p_tos`s' = 0 foreach k of var pre_tos`s' { capture confirm var `k' if c(rc) == 0 { replace p_tos`s' = `k' } } }
r(111);
For example, the first data set does not have variable pre_tos7. When I used the following codes, the following error appears:
HTML Code:
variable pre_tos7 not found r(111);
Could you please provide any advice?
Comment