Announcement

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

  • Creating a local list from a variable

    I have the following string values for two variables. I would like to create a local list from Var1 and/or Var2.

    clear
    input str4 Var1 str3 Var2
    "A f" "H O"
    "B" "L"
    "C" "Z"
    "D" "N t"
    "E g" "m o"
    "F" "a p"
    "G" "w"
    "" "q"
    "" "po"
    end
    [/CODE]

    when I use levels of this is what I get:
    levelsof Var1, local(levels)
    `"A f"' `"B"' `"C"' `"D"' `"E g"' `"F"' `"G"'

    local List1 I desire is:
    `" "A f" "B" "C" "D" "E g" "F" "G" "'

    Similarly,
    levelsof Var2, local(levels) is:
    `"H O"' `"L"' `"N t"' `"Z"' `"a p"' `"m o"' `"po"' `"q"' `"w"'

    local List2 I desire is:
    `" "H O" "L" "N t" "Z" "a p" "m o" "po" "q" "w" "'

    The goal is to eliminate manual entry to create List1 and and List2. Instead just grab them from the Var1 or Var2 and create a local list.

    Any help would be appreciated.
    Thanks


  • #2
    I do not understand what the list you describe as list1 provides that the list created by levelsof does not already give you. Consider the following:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 Var1 str3 Var2
    "A f" "H O"
    "B"   "L"  
    "C"   "Z"  
    "D"   "N t"
    "E g" "m o"
    "F"   "a p"
    "G"   "w"  
    ""    "q"  
    ""    "po" 
    end
    
    levelsof Var1, local(list1)
    macro list _list1
    display `"the local macro list1: `list1' "'
    foreach v of local list1 {
    macro list _v
    }
    Code:
    . levelsof Var1, local(list1)
    `"A f"' `"B"' `"C"' `"D"' `"E g"' `"F"' `"G"'
    
    . macro list _list1
    _list1:         `"A f"' `"B"' `"C"' `"D"' `"E g"' `"F"' `"G"'
    
    . display `"the local macro list1: `list1' "'
    the local macro list1: `"A f"' `"B"' `"C"' `"D"' `"E g"' `"F"' `"G"' 
    
    . foreach v of local list1 {
      2. macro list _v
      3. }
    _v:             A f
    _v:             B
    _v:             C
    _v:             D
    _v:             E g
    _v:             F
    _v:             G
    
    .
    Can you given an example of a command that does not work using the list created by the levelsof command? Perhaps the problem is not what you think it is, or perhaps there's a different way of solving the problem.

    Comment


    • #3
      William,
      Suppose I created the list1 using the levels of. Then I would like to compare another variable (names) and keep the observations that matches one of the elements in list1 but I get an error.


      clear
      input str4 Var1 str3 Var2 str4 names
      "A f" "H O" ""
      "B" "L" "A f"
      "C" "Z" ""
      "D" "N t" ""
      "E g" "m o" "F"
      "F" "a p" ""
      "G" "w" ""
      "" "q" ""
      "" "po" ""
      end

      the code i used was :
      foreach l of local list1 {
      keep if names ==`l'
      }

      it says "A not found". I am making a syntax error somewhere but cant figure out where. If I can keep the two observations in any other way preferably not using a loop that would be best.

      Comment


      • #4
        My example showed that when the list items are extracted one at a time into the local macro v, they are no longer surrounded in quotation marks. You need to add quotation marks to your code so that Stata recognizes "A f" as a character string. As written, after substitution itStata is trying to execute
        Code:
        keep if names ==A f
        What you want is
        Code:
        foreach l of local list1 {
        keep if names =="`l'"
        }
        Except that won't work - after the first time through the loop, all observations will be gone except for those where names=="A f", and after the second time through the loop, those too will have been deleted. What you want is something like the following.
        Code:
        generate tokeep = 0
        foreach l of local list1 {
        replace tokeep = tokeep | names =="`l'"
        }
        keep if tokeep
        Last edited by William Lisowski; 30 Dec 2019, 13:34.

        Comment

        Working...
        X