Announcement

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

  • generate variable that can change every year

    Hi guys, I wanna thank you first, cause I have received so much help here. This is such a great forum! I will try my best to help others as well.
    I have another question.I have a dataset of stock market index of 40 countries. And I need to generate the correlations between US and the other 39 countries.

    I tried the code below,

    bysort fyear: corr Australia_price US_price
    gen aus_us=r(rho)
    bysort fyear: corr Belgium_price US_price
    gen bel_us=r(rho)
    bysort fyear: corr Brazil_price US_price
    gen bra_us=r(rho)
    bysort fyear: corr Canada_price US_price
    gen can_us=r(rho)
    bysort fyear: corr Chile_price US_price
    gen chl_us=r(rho)
    bysort fyear: corr China_price US_price
    gen chn_us=r(rho)
    bysort fyear: corr Colombia_price US_price
    gen col_us=r(rho)
    bysort fyear: corr Cyprus_price US_price
    gen cyp_us=r(rho)
    bysort fyear: corr Denmark_price US_price
    gen dnk_us=r(rho)
    bysort fyear: corr Finland_price US_price
    gen fin_us=r(rho)
    bysort fyear: corr France_price US_price
    gen fra_us=r(rho)
    bysort fyear: corr Germany_price US_price
    gen ger_us=r(rho)
    bysort fyear: corr Greece_price US_price
    gen gre_us=r(rho)
    bysort fyear: corr HongKong US_price
    gen hkg_us=r(rho)
    bysort fyear: corr India_price US_price
    gen ind_us=r(rho)
    bysort fyear: corr Indonesia_price US_price
    gen idn_us=r(rho)
    bysort fyear: corr Ireland_price US_price
    gen irl_us=r(rho)
    bysort fyear: corr Isreal_price US_price
    gen isr_us=r(rho)
    bysort fyear: corr Italy_price US_price
    gen ita_us=r(rho)
    bysort fyear: corr Japan_price US_price
    gen jpn_us=r(rho)
    bysort fyear: corr Korea_price US_price
    gen kor_us=r(rho)
    bysort fyear: corr Mexico_price US_price
    gen mex_us=r(rho)
    bysort fyear: corr Netherlands_price US_price
    gen net_us=r(rho)
    bysort fyear: corr Norway_price US_price
    gen nor_us=r(rho)
    bysort fyear: corr Peru_price US_price
    gen per_us=r(rho)
    bysort fyear: corr Philippines_price US_price
    gen phl_us=r(rho)
    bysort fyear: corr Portugal_price US_price
    gen por_us=r(rho)
    bysort fyear: corr Russia_price US_price
    gen rus_us=r(rho)
    bysort fyear: corr Singapore_price US_price
    gen sng_us=r(rho)
    bysort fyear: corr SouthAfrica_price US_price
    gen sa_us=r(rho)
    bysort fyear: corr Spain_price US_price
    gen esp_us=r(rho)
    bysort fyear: corr Sweden_price US_price
    gen swd_us=r(rho)
    bysort fyear: corr Switzerland_price US_price
    gen swt_us=r(rho)
    bysort fyear: corr Taiwan_price US_price
    gen twn_us=r(rho)
    bysort fyear: corr Thailand_price US_price
    gen thl_us=r(rho)
    bysort fyear: corr UK_price US_price
    gen uk_us=r(rho)
    bysort fyear: corr Venezuela_price US_price
    gen ven_us=r(rho)

    I meant to generate 39 new variables of correlation, which change every year. But I only got the correlation in the last year at the end. I will upload picture the data.

    Thank you in advance!

  • #2
    Click image for larger version

Name:	capture.JPG
Views:	1
Size:	113.7 KB
ID:	1497986
    the data is very big, I cannot upload it here. Here is the picture.
    Last edited by Johnny YANG; 12 May 2019, 03:42.

    Comment


    • #3
      I have solved this myself. If you have any idea to improve this, you are welcome to comment too.

      corr Chile_price US_price if fyear==2001
      gen chl_us=r(rho)
      forval i=2002/2019 {
      corr Chile_price US_price if fyear==`i'
      replace chl_us=r(rho) if fyear==`i'
      }

      Comment

      Working...
      X