Announcement

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

  • code unique data as missing

    Greetings,

    Is there a Stata command to code unique data as missing? I am working with the basic version of stata and need to delete a child's school ID, teacher ID, and class ID if a child is a unique observation to keep only the repeating school IDs.

    these are the variables I want to code as missing "." if they are a unique identifier for a child: F1989SchID S1999SchID F1999SchID S2000SchID S2002SchID S2004SchID S2007SchID F98T_ID S99T_ID S00T_ID S02T_ID Sp04ReadT_ID Sp04MaT_ID Sp07EngT_ID Sp07MaT_ID Sp04ReadCLS_ID Sp04MaCLS_ID Sp07EngCLA_ID Sp07MaCLS_ID

    sorry if that is not clear! Thanks in advance.
    Attached Files

  • #2
    Something like this:

    Code:
    foreach v of varlist F1989SchID S1999SchID F1999SchID ///
    S2000SchID S2002SchID S2004SchID S2007SchID ///
    F98T_ID S99T_ID S00T_ID S02T_ID Sp04ReadT_ID ///
    Sp04MaT_ID Sp07EngT_ID Sp07MaT_ID Sp04ReadCLS_ID ///
    Sp04MaCLS_ID Sp07EngCLA_ID Sp07MaCLS_ID {
        capture confirm numeric var `v', exact
        if c(rc) == 0 {
            by `v', sort: replace `v' = . if _N == 1
        }
        else {
            by `v', sort: replace `v' = "" if _N == 1
        }
        
    }
    Please read the Forum FAQ for excellent advice on how to post questions in the most effective way. Among the things you will learn there, attachments are discouraged. Some people, myself included, will not download files from people we do not know. The most helpful way to show example data is by using the -dataex- command. If you are running version 18, 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.

    Because I have not accessed your attachment, I have written code based on how I imagine the data to look. If I have imagined incorrectly, the code may not work in your data. Of particular note, I have assumed that each child has only one record in the data set. If the same child can have multiple observations, then this code will fail to replace the values of its variables with missings. The code can be fairly easily modified to accommodate this situation, but it would be best to do that with usable example data in hand.

    Comment

    Working...
    X