Announcement

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

  • repeating the same observations for each cross section

    Hi everyone,

    I have a trade dataset by firm, product and destination. I would like to replicate the same product-destination pairs for each firm. My data is not rectangular so that's why I can't use the fillin command.

    Please find below a hypothetical example of my data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(firm product) str3 destination
    1 1 "EGY"
    1 1 "LYB"
    1 1 "PSE"
    2 1 "EGY"
    3 1 "LYB"
    1 2 "TUR"
    1 2 "FRA"
    1 2 "SPA"
    2 2 "TUR"
    3 2 "TUR"
    end
    And this is a hypothetical example from the product-destination data that I would like to replicate for each firm (and I have them in a separate dataset):
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte product str3 destination
    1 "EGY"
    1 "LYB"
    1 "PSE"
    2 "TUR"
    2 "FRA"
    2 "SPA"
    end
    And this is the end result that I would like to have:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(firm product) str3 destination
    1 1 "EGY"
    1 1 "LYB"
    1 1 "PSE"
    2 1 "EGY"
    2 1 "LYB"
    2 1 "PSE"
    3 1 "EGY"
    3 1 "LYB"
    3 1 "PSE"
    1 2 "TUR"
    1 2 "FRA"
    1 2 "SPA"
    2 2 "TUR"
    2 2 "FRA"
    2 2 "SPA"
    3 2 "TUR"
    3 2 "FRA"
    3 2 "SPA"
    end

    I would be grateful if you could advise on how to do this. Please note that my data is not rectangular so I can't use the fillin command.

    Many thanks in advance.

  • #2
    You can use cross to do this.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte product str3 destination
    1 "EGY"
    1 "LYB"
    1 "PSE"
    2 "TUR"
    2 "FRA"
    2 "SPA"
    end
    tempfile product_dest
    save `product_dest', replace
    
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(firm product) str3 destination
    1 1 "EGY"
    1 1 "LYB"
    1 1 "PSE"
    2 1 "EGY"
    3 1 "LYB"
    1 2 "TUR"
    1 2 "FRA"
    1 2 "SPA"
    2 2 "TUR"
    3 2 "TUR"
    end
    
    // Get a list of all unique firms
    keep firm
    duplicates drop
    
    // Generating dataset with all product-destination combinations for all firms
    cross using `product_dest'
    sort product firm destination

    Comment

    Working...
    X