Announcement

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

  • Copy values missings filter variable

    Dear Stata-Listers,

    i hope so much, you can help me. Here´s my problem: I would like to copy those values from Var1 to the other observation if ID is == FIlter.


    ID Group ID Filter Var1
    ------------------------------------------------------
    4 1 missing B
    5 1 4 missing
    6 1 4 missing
    7 2 missing C
    8 2 2 missing

    Thank u so much!!

  • #2
    Sorry here with a table again.

    ID group id Filter Var1
    4 1 missing B
    5 1 4 missing
    6 1 4 missing
    7 2 missing C
    8 2 7 missing








    Comment


    • #3
      I would like to copy those values from Var1 to the other observation if ID is == FIlter.
      ???
      Which values are "those values?"
      What observation is "the other observation?"
      Your data example does not include any instances where ID == Filter.

      Please clarify your question. I have no idea what you want. If it is difficult to describe in words, please post back with a better example and also show what you want the results to look like.

      Comment


      • #4
        Wait, I may have figured out what you want. Do you want to replace the values of var1 that are "missing" with the var1 values from the observations having id equal to the filter value in the current observation? If so, you can do that with:
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte(id groupid) str8 filter str7 var1
        4 1 "missing " "B"      
        5 1 "4 "       "missing"
        6 1 "4 "       "missing"
        7 2 "missing " "C"      
        8 2 "7 "       "missing"
        end
        
        //  CREATE PROPER STATA MISSING VALUES IN VARIABLES
        ds, has(type string)
        foreach v of varlist `r(varlist)' {
            replace `v' = "" if trim(itrim(`v')) == "missing"
        }
        
        //  MAKE filter NUMERIC
        destring filter, replace
        
        //  SOLVE PROBLEM
        frame put id var1, into(source)
        frame source: drop if missing(var1)
        frlink m:1 filter, frame(source id)
        replace var1 = frval(source, var1) if missing(var1)
        drop source
        frame drop source
        Note: Requires version 16 or later because it uses frames.

        Added: This code presumes that variable id uniquely identifies observations in the data. If that assumption fails in the real data, the code will break at the -frlink- command. So if Stata tells you that it cannot do the -frlink-, check your data to find and correct duplicate observations with the same value of id.

        In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

        Comment


        • #5
          Dear Mr. Schlechter,

          im sorry for the quality of my post. And Yes, you are absolutely right with "Do you want to replace the values of var1 that are "missing" with the var1 values from the observations having id equal to the filter value in the current observation?" But i have only version 14. Is there no solution for older stata versions than 16?

          THANKS!!

          Comment


          • #6
            What is done with frames can be done using a -tempfile- in older versions of Stata:
            Code:
            //  SOLVE PROBLEM
            preserve
            keep id var1
            drop if missing(var1)
            rename id filter
            tempfile source
            save `source'
            
            restore
            merge m:1 filter using `source', update nogenerate
            sort id
            Note: The preliminaries about missing values and making filter a numeric variable do not change from what was offered in #4.

            The Forum FAQ recommends always stating what version of Stata you are using if it is not the current version. That is designed to deal precisely with this kind of circumstance.

            Comment

            Working...
            X