Announcement

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

  • Multiplication of variables group

    Dear Stata mambers,

    I have a dataset with ca 400 variables over 10 years. The dataset consists of two groups of variables, each sharing same prefix followed by County-ISO-Code (Stat_* and Tr_* with * for US, DE etc.).
    I need to generate variables for each pair of ISO-Codes (e.g. Stat_DE*Tr_DE).

    There is a solution for similar problem: http://www.stata.com/statalist/archi.../msg00402.html
    Unfortunately unlike the example the groups in my dataset are not equal in size - there are more Stat_* variables than Sp_*.

    Thank you for your help.

  • #2
    You don't give much away (sample data with dataex (SSC) would have helped) but it seems that this is soluble. Let's fake two sets of suffixes that are not identical. Then we can get two sets of suffixes by using unab to give the corresponding varlists but zapping all the prefixes. Then the valid set of suffixes is the intersection of the two sets.

    Code:
     
    set obs 1 
    
    gen Stat_DE = 42 
    gen Stat_FR = 42 
    gen Stat_GB = 42 
    
    gen Tr_DE = _pi 
    gen Tr_FR = _pi 
    gen Tr_IT = _pi 
    
    unab Tr : Tr_* 
    local Tr: subinstr local Tr "Tr_" "", all 
    unab Stat : Stat_*    
    local Stat: subinstr local Stat "Stat_" "", all 
    
    local suffixes : list Tr & Stat 
    
    foreach s of local suffixes { 
        gen Prod_`s' = Stat_`s' * Tr_`s' 
    } 
    
    list Prod_* 
    
         +---------------------+
         |  Prod_DE    Prod_FR |
         |---------------------|
      1. | 131.9469   131.9469 |
         +---------------------+
    Clearly IT and GB for which we have only one variable each don't appear in the intersection.

    Here is another way to do it, which just ignores errors:

    Code:
     
    foreach v of var Stat_* { 
        local s : subinstr local v "Stat_" "", all 
        capture gen prod_`s' = Stat_`s' * Tr_`s' 
    } 
    
    list prod_*


    Comment


    • #3
      Nick, thank you for your help.

      Comment

      Working...
      X