Announcement

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

  • Yanyu Li
    started a topic using foreach get mean in panel data

    using foreach get mean in panel data

    I am thinking is it possible to get mean across various data within each day?

    for example, I have the following sample:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Name double(UT1 UT2 UT3)
    10959 -.0073132214800006754                      0 .
    10960   .010169472770298055     .01945556929411236 .
    10961  .0072498282100867196    .008194263889847106 .
    10962  -.005170111965564398    -.00562484648144707 .
    10965  -.002589907056536564   .0010243339747692233 .
    10966  -.004948799525782424   -.002049718285617035 .
    10967  -.001047186306347277  -.0033377841515116807 .
    10968  -.004452292301003223  -.0010298703193938963 .
    10969 -.0031516672463765554   -.006724155666651714 .
    10972  -.012717640663606661   -.009120310733434348 .
    10973 -.0005347650904454377   -.005517277823359298 .
    10974  .0013256929755434738                      0 .
    10975  -.005603395975109575   .0015818014631705153 .
    10976 -.0034871366581989095   -.005539094086807045 .
    10979 -.0026990278298278008   .0023811580727798793 .
    10980  -.010574518872111149   -.007144088554374894 .
    10981  -.009032454005356707   -.009877072860297155 .
    10982   .009578585767754002    .009877072860297117 .
    10983 -.0005461317623971705   .0005266796263820444 .
    10986   .006794451225562484    .006086026708618112 .
    10987 -.0013573753706163474 -.00028624588717003055 .
    I know if I reshape the data to long, I can use -by time, sort:- to calculate mean of variables within each day.

    But I want to try -for each- code cause I don't want to reshape to long, then reshape to wide. Could anyone give me some suggestions, please?
    I am really grateful!

    Best,

    Yanyu

  • Yanyu Li
    replied
    AAA~~~ YES! I am so stupid ~

    Big THX!

    Best~

    Leave a comment:


  • Nick Cox
    replied
    Code:
    summarize UT*

    Leave a comment:


  • Yanyu Li
    replied
    but, Nick, what if I want to calculate mean(UT1) mean(UT2) mean(UT3)... separately over whole research period?

    Really sorry for so many questions.

    Best!

    Leave a comment:


  • Yanyu Li
    replied
    A~~~ I see~

    Many thanks, Nick!

    Best~

    Leave a comment:


  • Nick Cox
    replied
    You want rowmean() not mean().

    Leave a comment:


  • Yanyu Li
    replied
    Dear Nick,

    Sorry for my unclear explanation.

    I want to do:

    by each day (Name indicate date), I calculate mean of UT=(UT1+UT2+UT3+...+UT262)/262

    I tried to use code:
    Code:
    . by Name, sort: egen mean_UT=mean(UT*)
    UT* invalid name
    r(198);
    
    . egen mean_UT=mean(UT`*'), by(Name)
    UT ambiguous abbreviation
    r(111);
    So, I think maybe I should use -foreach- to identify the number of UT.

    Many THX!

    Leave a comment:


  • Nick Cox
    replied
    I don't understand how your example data relates to your question. But examples like

    Code:
    webuse grunfeld, clear
    egen mean_invest = mean(invest), by(year)
    tabdisp year, c(mean_invest)
    tab year, su(invest)
    tabstat invest,by(year)
    show that you can get means for groups of observations in various ways -- without any loops.

    Leave a comment:

Working...
X