Announcement

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

  • Loop using the content of another data frame

    Hi there!

    Sorry if the question has already been answered. Unfortunately, I couldn't find the solution in the other posts.

    I have two data sets: one main data set (say dataset_1), which I want to do the following loop on one of the variables:

    foreach x in local list_name {

    replace imp_name = "`x'" if ustrregexm(imp_address, "`x'")

    }

    The issue I have is that I want to use the content of a variable from another dataset (say variable names in dataset_2) to create my macro, which is local list_name and it is a long list and I don't want to put all the names in my code.

    Any help is appreciated.



  • #2
    I don't quite follow what specific problem you are trying to address, but reading your pseudo-code, let me make some comments and provide a possible solution. If the solution is not what you are looking for, then please follow the advice of the FAQ and provide us a data example from both frames, along with an example of what you want the data to look like in the end.

    1) Your loop contents is doing nothing more than a (case-insensitive) string match. Let's assume letter casing is not an issue.
    2) Your loop is simply replacing the value of a string variable with the name that matches from the other frame. But this step is redundant since you already have the name variable, and just need to know which ones exist in both frames.

    The effect of your problem is to find a match in frame 1 if any contents from one variable are found in frame 2. This is best accomplished with a merge operation.

    Code:
    clear all
    
    frame create One
    frame change One
    input str10(name)
    Alice
    Mike
    Charles
    Diana
    end
    
    frame create Two
    frame change Two
    input str10(name_list)
    Alice
    Bob
    Charles
    Diana
    Evelyn
    Frank
    George
    Heather
    Ira
    John
    Ken
    Leonardo
    Mike
    end
    
    frame change One
    frlink 1:1 name , frame(Two name_list) gen(_name)
    gen byte has_match = !missing(_name)
    list, abbrev(12)
    Result

    Code:
    . list, abbrev(12)
    
         +-----------------------------+
         |    name   _name   has_match |
         |-----------------------------|
      1. |   Alice       1           1 |
      2. |    Mike      13           1 |
      3. | Charles       3           1 |
      4. |   Diana       4           1 |
         +-----------------------------+

    Comment


    • #3
      I agree with Leonardo that frlink is the natural way of combining data from two frames.

      I want to make the point, though, that frames hold data only. Any local macros created in a frame are not limited to that frame.
      Code:
      clear all 
      cls
      clear all
      // frame Two
      frame create Two
      frame change Two
      input str10(names)
      Alice
      Mike
      Charles
      Diana
      end
      levelsof names, local(list_name)
      // default frame
      frame change default
      foreach x of local list_name {
          display "`x'"
      }
      Code:
      . clear all
      
      . // frame Two
      . frame create Two
      
      . frame change Two
      
      . input str10(names)
      
                names
        1. Alice
        2. Mike
        3. Charles
        4. Diana
        5. end
      
      . levelsof names, local(list_name)
      `"Alice"' `"Charles"' `"Diana"' `"Mike"'
      
      . // default frame
      . frame change default
      
      . foreach x of local list_name {
        2.     display "`x'"
        3. }
      Alice
      Charles
      Diana
      Mike
      
      .

      Comment

      Working...
      X