Announcement

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

  • Interact variables in order to create new ones in time series

    Hi everyone,

    I would like to create a database which contain all informations about some prudential measures which have been implemented by prudential authorities. The original database describe each measure, one per row.

    This is a short sample of what I have so far:

    Code:
    clear
    input str2 country str6 announce_date type str6 inforce_date national 
    FR 1991m1 1 1991m3 1
    FR 1991m4 1 1991m5 1
    ES 1992m4 2 1992m5 1
    ES 1992m6 1 1992m6 0
    end
    Here, the first row tell us that this measure is a type 1 measure in France. It was announced in January 1991, and then implemented in March 1991. This measure applied to the whole country ("national" is a dummy which take a value of 1 if the measure is national).

    I have been asked to do this transformation:

    Code:
    clear
    input str6 Date FR_T1_ANN_NA ES_T1_ANN_NA FR_T2_ANN_NA ES_T2_ANN_NA  FR_T1_INF_NA
    1991m1 1 0 0 0 0
    1991m2 0 0 0 0 0
    1991m3 0 0 0 0 1
    1991m4 1 0 0 0 0
    1991m5 0 0 0 0 1
    1991m6 . . . . .
    1991m7 . . . . .
    1991m8 . . . . .
    1991m9 . . . . .
    1991m10 . . . . .
    1991m12 . . . . .
    1992m1 . . . . .
    1992m2 . . . . .
    1992m3 . . . . .
    1992m4 . . . . .
    1992m5 . . . . .
    1992m6 . . . . .
    end
    Here, "." are not missing values, it's more a "and so on". In addition, I only entered few variables, but there is more of them.
    With this format, we will have a variable "Date", which is the time variable (1991m1 to 2018m12).

    All of the variables except "Date" are dummies. For example, "FR_T1_ANN_NA" = 1 give us the same information as the first row of the first database that I described. The only difference is that we only have the announcement of the measure, so we need to give a value of 1 for FR_T1_INF_NA in 1991m3 as well ("FR" = France, "T1" = type 1 instrument, "INF" = In force", "NA" = national"). In the end, there will be a huge number of variables, because each one describe a specific measure.
    So the idea is to interact all variables related to prudential measures, in order to keep all information.

    In addition, I would like to know if it is possible to name these variables automatically, in a way that I will know what they mean, such as "FR_T1_ANN_NA".
    I know the command
    Code:
    tab variable, gen(variable)
    but that would give me names such as "variable1", "variable2", "variable3" etc.

    If anybody has an idea if and how this can be achieved, I would be very grateful.


  • #2
    While I have considerable difficulty imagining why you want to do this, and even greater difficulty imagining that you will not regret it once you have, here is how to do it:

    Code:
    clear
    input str2 country str6 announce_date type str6 inforce_date national 
    FR 1991m1 1 1991m3 1
    FR 1991m4 1 1991m5 1
    ES 1992m4 2 1992m5 1
    ES 1992m6 1 1992m6 0
    end
    
    gen long obs_no = _n
    reshape long @date, i(obs_no) j(event) string
    replace event = substr(event, 1, 3)
    tostring type national, replace
    replace type = "t"+type
    replace national = "na"+national
    
    egen label = concat(country type event national), punct("_")
    replace obs_no = 1
    
    keep obs_no label date
    reshape wide obs_no, i(date) j(label) string
    mvencode obs_no*, mv(0)
    rename obs_no* *
    Just remember that almost everything in Stata is easier, and sometimes only possible, with the data arrayed in long layout. The kind of wide layout you have asked for, and the code here creates, is seldom useful for data management or analysis in Stata.

    Comment


    • #3
      Thank you Clyde!

      Comment

      Working...
      X