Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to fill in by group missing variable obs with existing ones

    Hi everyone,

    I have merged in variables with the regions and sub regions for each country into my dataset but in the merged in data there was only data for very few years per country and I want to fill in the rest of the observations so that I have the region value for every observation. To do that I used this code:

    local regionvars region_un region_un_sub region_wb
    foreach var of regionsvars {
    by CountryName: replace `var' = firstnm(`var')
    }
    but it didn't work. What can I do instead?

    In case what I want to do isnt clear, here is a picture of my dataset. What I want to do is fill in the missing values for region_un and a few other regional variables for every country.

    Click image for larger version

Name:	Capture.JPG
Views:	1
Size:	44.4 KB
ID:	1505658

    Help would be awesome on this. Thanks!

  • #2
    Please refer to the FAQs on how to post data examples using the dataex command. For string variables, missing values are sorted first (the opposite is true for numerical variables). I can just about make that the variable "region_un" is numerical with value labels. Therefore, you want to specify a condition as follows:

    Code:
    bys CountryName (region_un): replace region_un=region_un[1]
    Last edited by Andrew Musau; 02 Jul 2019, 13:27.

    Comment

    Working...
    X