Announcement

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

  • merging two files when date is daily and yearly, respectively

    Dear All,

    I have two datasets on indicators for the same companies. Ind is an indicator measured daily which I have in the first file.Ind2 is an indicator measured at the end of the year. I wanted to merge this data. The problem is when I type: merge Company using file2 I don't get the right values for Ind2. I want to tell stata to take Ind2 for every day for a given year. Since my data is sensitive I created an example how it looks like:

    file1:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int Time str1 Company double Ind
    14976 "A" .43
    14976 "B"   .
    14976 "C"   0
    14977 "A" .44
    14977 "B"   .
    14977 "C"   0
    14978 "A" .45
    14978 "B"   .
    14978 "C"   0
    14979 "A" .46
    14979 "B"   .
    14979 "C"   .
    14980 "A" .47
    14980 "B"   .
    14980 "C"   .
    14981 "A" .48
    14981 "B"   .
    14981 "C"   .
    14982 "A" .49
    14982 "B"   .
    14982 "C"   .
    14983 "A"  .5
    14983 "B" .23
    14983 "C"   .
    14984 "A" .51
    14984 "B" .23
    14984 "C"   .
    14985 "A" .52
    14985 "B" .23
    14985 "C"   .
    14986 "A" .53
    14986 "B" .23
    14986 "C"   .
    14987 "A" .54
    14987 "B" .23
    14987 "C"   .
    14988 "A" .55
    14988 "B" .24
    14988 "C"   .
    14989 "A" .56
    14989 "B" .25
    14989 "C"   .
    14990 "A" .57
    14990 "B" .26
    14990 "C"   .
    14991 "A" .58
    14991 "B" .27
    14991 "C"   .
    14992 "A" .59
    14992 "B" .28
    14992 "C"   .
    14993 "A"  .6
    14993 "B" .29
    14993 "C"   .
    14994 "A" .61
    14994 "B"  .3
    14994 "C"   .
    14995 "A" .62
    14995 "B" .31
    14995 "C"   .
    14996 "A" .63
    14996 "B" .32
    14996 "C"   .
    14997 "A" .64
    14997 "B" .33
    14997 "C"   .
    14998 "A" .65
    14998 "B" .34
    14998 "C"   .
    14999 "A" .66
    14999 "B" .35
    14999 "C"   .
    15000 "A" .67
    15000 "B" .36
    15000 "C"   .
    15001 "A" .68
    15001 "B" .37
    15001 "C"   .
    15002 "A" .69
    15002 "B" .38
    15002 "C"   .
    15003 "A"  .7
    15003 "B" .39
    15003 "C"   .
    15004 "A" .71
    15004 "B"  .4
    15004 "C"   .
    15005 "A" .72
    15005 "B" .41
    15005 "C"   .
    15006 "A" .73
    15006 "B" .42
    15006 "C"   .
    15007 "A" .74
    15007 "B" .43
    15007 "C"   .
    15008 "A" .75
    15008 "B" .44
    15008 "C"   .
    15009 "A" .76
    end
    format %tdnn/dd/CCYY Time
    The file 2:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int Time str1 Company double Ind2
    17501 "A"  .002
    17501 "B"   .12
    17501 "C"   .03
    17867 "A"  .003
    17867 "B"   .13
    17867 "C"   .05
    18232 "A"  .002
    18232 "B"   .14
    18232 "C"   .07
    18597 "A"  .005
    18597 "B"   .15
    18597 "C"   .09
    18962 "A"  .005
    18962 "B"   .16
    18962 "C"   .11
    19328 "A" .0058
    19328 "B"   .17
    19328 "C"   .13
    19693 "A" .0066
    19693 "B"   .18
    19693 "C"   .15
    20058 "A" .0074
    20058 "B"   .19
    20058 "C"   .17
    20423 "A" .0082
    20423 "B"    .2
    20423 "C"   .19
    20789 "A"  .009
    20789 "B"   .21
    20789 "C"   .21
    end
    format %tdMon-YY Time
    I wanna have for example for the company A for every day in a given year the same Ind2.

    I would be very garteful for your help

  • #2
    Create a year variable from the date variable in each dataset and then merge m:1 on both company and year. Also, rename "Time" to, e.g., "Time2" in one of the datasets so there is no name clash while merging.

    Code:
    gen year= year(Time)
    merge m:1 Company year using dataset2

    Comment


    • #3
      Thank you for your kind help

      Comment

      Working...
      X