Many thanks to all Statalist contributors, whose posts and references in many topics have helped me hundreds of times for the two years I have been using Stata.
This time I have come to a deadlock.
I am currently using Stata13.
I have a dataset (see dataex below) where each observation is defined by the day of production ("date" variable), the country of production ("loc" variable), the category of output ("category" variable) and the number of output units ("units" variable). I have such observations only when the triplet (day, country, category) refers to at least one output unit, otherwise there is no corresponding observation in the dataset. My problem is that I would like to add the latter corresponding observations to the dataset, with "0" as number of output units. The (day, country, category) triplets for the observations to be added would be inferred from the existing observations and the combinatory of all unique values taken by the variables "date", "loc" and "category".
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int date str2 loc str9 category float units 19724 "AD" "cars" 1 19724 "AG" "cars" 1 19724 "AL" "cars" 3 19724 "AO" "cars" 1 19724 "AR" "crops" 1 19724 "AR" "cars" 10 19724 "AT" "cars" 2 19724 "AT" "tables" 1 19724 "AU" "crops" 5 19724 "AU" "cars" 91 19724 "AU" "tables" 13 19724 "BB" "cars" 4 19724 "BB" "tables" 2 19724 "BD" "cars" 4 19724 "BE" "cars" 13 19724 "BE" "tables" 2 19724 "BF" "cars" 4 19724 "BG" "cars" 1 19724 "BH" "cars" 1 19724 "BH" "tables" 2 19724 "BJ" "cars" 4 19724 "BO" "cars" 4 19724 "BR" "crops" 3 19724 "BR" "cars" 55 19724 "BR" "tables" 11 19724 "BS" "crops" 2 19724 "BS" "cars" 14 19724 "BS" "tables" 1 19724 "BT" "cars" 1 19724 "BW" "cars" 8 19724 "BZ" "cars" 1 19724 "CA" "crops" 7 19724 "CA" "cars" 171 19724 "CA" "tables" 34 19724 "CD" "crops" 1 19724 "CD" "cars" 32 19724 "CD" "tables" 4 19724 "CF" "cars" 14 19724 "CF" "tables" 2 19724 "CG" "cars" 1 19724 "CH" "crops" 4 19724 "CH" "cars" 44 19724 "CH" "tables" 1 19724 "CI" "cars" 3 19724 "CL" "crops" 1 19724 "CL" "cars" 19 19724 "CL" "tables" 2 19724 "CM" "cars" 1 19724 "CN" "cars" 2 19724 "CN" "tables" 2 19724 "CO" "crops" 2 19724 "CO" "cars" 81 19724 "CO" "tables" 7 19724 "CR" "cars" 5 19724 "CU" "cars" 4 19724 "CU" "tables" 2 19724 "CY" "cars" 2 19724 "CZ" "cars" 5 19724 "DE" "cars" 37 19724 "DE" "tables" 4 19724 "DK" "cars" 5 19724 "DK" "tables" 1 19724 "DO" "cars" 3 19724 "DZ" "cars" 1 19724 "DZ" "tables" 1 19724 "EC" "crops" 1 19724 "EC" "cars" 16 19724 "EC" "tables" 7 19724 "EG" "cars" 13 19724 "EG" "tables" 1 19724 "ES" "cars" 30 19724 "ES" "tables" 6 19724 "ET" "cars" 5 19724 "FI" "crops" 1 19724 "FI" "cars" 14 19724 "FI" "tables" 1 19724 "FM" "cars" 1 19724 "FR" "crops" 4 19724 "FR" "cars" 63 19724 "FR" "tables" 5 19724 "GB" "crops" 5 19724 "GB" "cars" 167 19724 "GB" "tables" 26 19724 "GD" "cars" 1 19724 "GE" "cars" 8 19724 "GH" "cars" 6 19724 "GM" "cars" 27 19724 "GM" "tables" 2 19724 "GN" "cars" 3 19724 "GR" "crops" 1 19724 "GR" "cars" 5 19724 "GT" "cars" 2 19724 "GY" "cars" 1 19724 "HK" "crops" 2 19724 "HK" "cars" 7 19724 "HN" "cars" 5 19724 "HR" "cars" 2 19724 "HU" "cars" 7 19724 "ID" "crops" 3 19724 "ID" "cars" 94 end format %td date
19724 "AD" "tables" 0
19724 "AD" "crops" 0
19724 "AG" "tables" 0
19724 "AG" "crops" 0
My first intuition was to use the expand function before correcting the newly created observations (for variables "loc", "category" and "units") by means of the replace function together with a conditional structure. However, I can't manage to get anywhere.
If anyone had a solution, even a clue, I would be very grateful.
Thank you for your time,
Adrien
0 Response to Adding new observations, partially similar to existing ones and according their pattern
Post a Comment