Announcement

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

  • Passing id list to local variable

    Hi guys,

    I'm currently working on identifying some weird values and outputting them for manual inspection. So, the dataset of all problematic subjects that I have will be like this:
    id var1 var2 va3
    a 5 6 4
    b 4 5 5
    c 5 4 3
    d 6 5 2
    What I want to do is to put all id in this dataset into a local variable/list, so that I can do something like this

    Code:
    local chklist XXXX
    foreach var in `chklist' {
        ...
    }
    My question is: how to extract the id list so that I can pass them into a local variable/list?

    While I'm trying to find the answer by myself, any suggestion/article/reference from you guys would definitely be helpful.

    Thanks in advance,
    Chung-Li

  • #2
    It is not clear what you seek - to loop over values of id or to loop over variable names. Perhaps one of these examples will be helpful to you.
    Code:
    . levelsof id, local(chklist_id)
    `"a"' `"b"' `"c"' `"d"'
    
    . foreach i in `chklist_id' {
      2.     display "checking id `i'"
      3. }
    checking id a
    checking id b
    checking id c
    checking id d
    
    . unab chklist_var : var1-var3
    
    . foreach v in `chklist_var' {
      2.     display "checking variable `v'"
      3. }
    checking variable var1
    checking variable var2
    checking variable var3
    
    .

    Comment


    • #3
      This reference provides good concept for the solution:
      HTML Code:
      https://www.stata.com/support/faqs/data-management/try-all-values-with-foreach/
      here's what I have for the solution:
      Code:
      levelsof iid, local(levels)
      
      foreach l of local levels{
          MyUse checklist
          keep if iid == "`l'"
          save "`l'.dta", replace
      }
      welcome if there's an alternative way, thanks!

      Comment


      • #4
        I think I have a different guess about your situation than William Lisowski, but both of us are guessing because your description is pretty thin in content. So, my thinking is that you have certain variables whose values indicate that an observation is a potential problem, and you want to inspect the observations with those problem values. If that is true, I don't think a list of id numbers is useful. I would instead create a binary variable to the observations of interest, and then browse or list or save a new data file conditioned on that variable. Perhaps you are having a difficulty defining that problem variable, but because you don't describe the relevant conditions for it, we can't help with that.
        Code:
        gen byte problem =  ... 0/1 problem variable defined by variables' values...
        keep if problem == 1
        save MyFileOfBadObservations.dta

        Comment


        • #5
          Originally posted by William Lisowski View Post
          It is not clear what you seek - to loop over values of id or to loop over variable names. Perhaps one of these examples will be helpful to you.
          Code:
          . levelsof id, local(chklist_id)
          `"a"' `"b"' `"c"' `"d"'
          
          . foreach i in `chklist_id' {
          2. display "checking id `i'"
          3. }
          checking id a
          checking id b
          checking id c
          checking id d
          
          . unab chklist_var : var1-var3
          
          . foreach v in `chklist_var' {
          2. display "checking variable `v'"
          3. }
          checking variable var1
          checking variable var2
          checking variable var3
          
          .
          Thanks, William. Sorry for my ambiguity. What you do is exactly what I'm looking for!

          Comment


          • #6
            Originally posted by Mike Lacy View Post
            I think I have a different guess about your situation than William Lisowski, but both of us are guessing because your description is pretty thin in content. So, my thinking is that you have certain variables whose values indicate that an observation is a potential problem, and you want to inspect the observations with those problem values. If that is true, I don't think a list of id numbers is useful. I would instead create a binary variable to the observations of interest, and then browse or list or save a new data file conditioned on that variable. Perhaps you are having a difficulty defining that problem variable, but because you don't describe the relevant conditions for it, we can't help with that.
            Code:
            gen byte problem = ... 0/1 problem variable defined by variables' values...
            keep if problem == 1
            save MyFileOfBadObservations.dta
            Thanks, Mike. Sorry for my ambiguity. Your solution helps! (it's actually implemented in the second part of my process)


            I've already got all ids that are potentially problematic from dataset A, and, say, I saved them in dataset B. What I want to do is to ask Stata to grab information from dataset C and D by each id stored in dataset B. Processing flow will be like this:

            dataset A: ids for all participants
            -> dataset B: ids for potentially problematic participants
            -> dataset C and D: the other two datasets contain different study info for all participants
            -> keep if id=id in dataset B (one by one) for dataset C and D


            Comment


            • #7
              What you describe can likely be done using the merge command to merge dataset B to datasets C and D.

              Comment

              Working...
              X