Announcement

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

  • Using local macros in tsline

    Hi,

    I am using Stata 16 and would need help in macros. Below is my dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 observation_date int unemploy long payems float unrate long(ce16ov cnp16ov clf16ov) byte usrec float(empop empop2554) byte(peaks troughs plucking_rec) float(date datem d_empop2554 d_empop)
    "1948-01-01" 2034 44679 3.4 58061 102603 60095 0 56.6 62.6 1 0 0 -4383 -144          .          .
    "1948-02-01" 2328 44533 3.8 58196 102698 60524 0 56.7 62.7 0 0 1 -4352 -143  .10000229  .10000229
    "1948-03-01" 2399 44683   4 57671 102771 60070 0 56.1 62.6 0 0 1 -4323 -142 -.10000229  -.6000023
    "1948-04-01" 2386 44379 3.9 58291 102831 60677 0 56.7 62.9 0 0 1 -4292 -141  .30000305   .6000023
    "1948-05-01" 2118 44796 3.5 57854 102923 59972 0 56.2 62.5 0 0 1 -4262 -140  -.4000015        -.5
    "1948-06-01" 2214 45034 3.6 58743 102992 60957 0   57 63.2 0 0 1 -4231 -139   .7000008   .7999992
    "1948-07-01" 2213 45160 3.6 58968 103216 61181 0 57.1 63.7 0 0 1 -4201 -138         .5  .09999847
    "1948-08-01" 2350 45178 3.9 58456 103240 60806 0 56.6   63 0 0 1 -4170 -137  -.7000008        -.5
    "1948-09-01" 2302 45294 3.8 58513 103291 60815 0 56.6 63.4 0 0 1 -4139 -136   .4000015          0
    "1948-10-01" 2259 45245 3.7 58387 103361 60646 0 56.5 63.1 0 0 1 -4109 -135 -.30000305 -.09999847
    "1948-11-01" 2285 45192 3.8 58417 103424 60702 0 56.5   63 0 0 1 -4078 -134 -.09999847          0
    "1948-12-01" 2429 45032   4 58740 103468 61169 1 56.8 63.1 0 0 1 -4048 -133  .09999847   .2999992
    "1949-01-01" 2596 44668 4.3 58175 103529 60771 1 56.2 62.7 0 0 1 -4017 -132  -.3999977  -.5999985
    "1949-02-01" 2849 44497 4.7 58208 103559 61057 1 56.2 62.6 0 0 1 -3986 -131 -.10000229          0
    "1949-03-01" 3030 44240   5 58043 103665 61073 1   56 62.5 0 0 1 -3958 -130 -.09999847 -.20000076
    "1949-04-01" 3260 44236 5.3 57747 103739 61007 1 55.7 62.2 0 0 1 -3927 -129  -.2999992  -.2999992
    "1949-05-01" 3707 43984 6.1 57552 103845 61259 1 55.4 62.2 0 0 1 -3897 -128          0  -.2999992
    "1949-06-01" 3776 43739 6.2 57172 103930 60948 1   55   62 0 0 1 -3866 -127 -.20000076  -.4000015
    "1949-07-01" 4111 43531 6.7 57190 104042 61301 1   55 61.9 0 0 1 -3836 -126 -.09999847          0
    "1949-08-01" 4193 43624 6.8 57397 104121 61590 1 55.1   62 0 0 1 -3805 -125  .09999847  .09999847
    "1949-09-01" 4049 43780 6.6 57584 104219 61633 1 55.3 61.9 0 0 1 -3774 -124 -.09999847  .20000076
    "1949-10-01" 4916 42942 7.9 57269 104338 62185 1 54.9 61.3 0 1 1 -3744 -123  -.6000023  -.3999977
    "1949-11-01" 3996 43242 6.4 58009 104421 62005 0 55.6   62 0 0 0 -3713 -122   .7000008   .6999969
    "1949-12-01" 4063 43522 6.6 57845 104524 61908 0 55.3 61.7 0 0 0 -3683 -121  -.2999992  -.2999992
    "1950-01-01" 4026 43526 6.5 57635 104619 61661 0 55.1 61.6 0 0 0 -3652 -120 -.10000229 -.20000076
    "1950-02-01" 3936 43297 6.4 57751 104737 61687 0 55.1 61.6 0 0 0 -3621 -119          0          0
    "1950-03-01" 3876 43954 6.3 57728 104844 61604 0 55.1 61.7 0 0 0 -3593 -118  .10000229          0
    "1950-04-01" 3575 44382 5.8 58583 104943 62158 0 55.8 62.4 0 0 0 -3562 -117   .7000008   .7000008
    "1950-05-01" 3434 44718 5.5 58649 105014 62083 0 55.8 62.6 0 0 0 -3532 -116  .19999695          0
    "1950-06-01" 3367 45083 5.4 59052 105104 62419 0 56.2 63.3 0 0 0 -3501 -115   .7000008   .4000015
    "1950-07-01" 3120 45454   5 59001 105194 62121 0 56.1   63 0 0 0 -3471 -114  -.2999992 -.10000229
    "1950-08-01" 2799 46192 4.5 59797 105282 62596 0 56.8 63.7 0 0 0 -3440 -113   .7000008   .7000008
    "1950-09-01" 2774 46438 4.4 59575 105269 62349 0 56.6 63.4 0 0 0 -3409 -112  -.2999992 -.20000076
    "1950-10-01" 2625 46706 4.2 59803 105096 62428 0 56.9 63.7 0 0 0 -3379 -111   .2999992  .30000305
    "1950-11-01" 2589 46776 4.2 59697 104979 62286 0 56.9 63.7 0 0 0 -3348 -110          0          0
    "1950-12-01" 2639 46861 4.3 59429 104872 62068 0 56.7 63.4 0 0 0 -3318 -109  -.2999992 -.20000076
    "1951-01-01" 2305 47288 3.7 59636 104844 61941 0 56.9   64 0 0 0 -3287 -108   .5999985  .20000076
    "1951-02-01" 2117 47577 3.4 59661 104604 61778 0   57   64 0 0 0 -3256 -107          0  .09999847
    "1951-03-01" 2125 47873 3.4 60401 104629 62526 0 57.7 64.4 0 0 0 -3228 -106   .4000015   .7000008
    "1951-04-01" 1919 47861 3.1 59889 104541 61808 0 57.3 64.3 0 0 0 -3197 -105 -.09999847  -.4000015
    "1951-05-01" 1856 47952   3 60188 104491 62044 0 57.6 64.5 0 0 0 -3167 -104  .19999695   .2999992
    "1951-06-01" 1995 48064 3.2 59620 104488 61615 0 57.1 63.8 0 0 0 -3136 -103  -.7000008        -.5
    "1951-07-01" 1950 48061 3.1 60156 104504 62106 0 57.6 64.8 0 0 0 -3106 -102  1.0000038         .5
    "1951-08-01" 1933 48012 3.1 59994 104536 61927 0 57.4 64.5 0 0 0 -3075 -101 -.30000305 -.19999695
    "1951-09-01" 2067 47954 3.3 59713 104588 61780 0 57.1 64.3 0 0 0 -3044 -100 -.19999695 -.30000305
    "1951-10-01" 2194 48006 3.5 60010 104690 62204 0 57.3 64.7 0 0 0 -3014  -99   .3999939  .20000076
    "1951-11-01" 2178 48147 3.5 59836 104740 62014 0 57.1 64.6 0 0 0 -2983  -98 -.09999847 -.20000076
    "1951-12-01" 1960 48314 3.1 60497 104810 62457 0 57.7   65 0 0 0 -2953  -97   .4000015   .6000023
    "1952-01-01" 1972 48296 3.2 60460 104862 62432 0 57.7 65.1 0 0 0 -2922  -96  .09999847          0
    "1952-02-01" 1957 48522 3.1 60462 104868 62419 0 57.7 65.2 0 0 0 -2891  -95  .09999847          0
    "1952-03-01" 1813 48504 2.9 59908 104860 61721 0 57.1 65.2 0 0 0 -2862  -94          0  -.6000023
    "1952-04-01" 1811 48620 2.9 59909 104906 61720 0 57.1   65 0 0 0 -2831  -93 -.19999695          0
    "1952-05-01" 1863 48642   3 60195 104996 62058 0 57.3 65.1 0 0 0 -2801  -92  .09999847  .20000076
    "1952-06-01" 1884 48282   3 60219 105118 62103 0 57.3 64.9 0 0 0 -2770  -91 -.19999695          0
    "1952-07-01" 1991 48143 3.2 59971 105246 61962 0   57 64.7 0 0 0 -2740  -90 -.20000458  -.2999992
    "1952-08-01" 2087 48924 3.4 59790 105346 61877 0 56.8 64.5 0 0 0 -2709  -89 -.19999695 -.20000076
    "1952-09-01" 1936 49320 3.1 60521 105436 62457 0 57.4 65.3 0 0 0 -2678  -88   .8000031   .6000023
    "1952-10-01" 1839 49597   3 60132 105591 61971 0 56.9 64.7 0 0 0 -2648  -87  -.6000061        -.5
    "1952-11-01" 1743 49816 2.8 60748 105706 62491 0 57.5 65.2 0 0 0 -2617  -86         .5   .5999985
    "1952-12-01" 1667 50166 2.7 60954 105812 62621 0 57.6 65.5 0 0 0 -2587  -85  .30000305  .09999847
    "1953-01-01" 1839 50144 2.9 61600 106594 63439 0 57.8 65.7 0 0 0 -2556  -84  .19999695  .20000076
    "1953-02-01" 1636 50339 2.6 61884 106678 63520 0   58 65.8 0 0 0 -2525  -83   .1000061  .20000076
    "1953-03-01" 1647 50473 2.6 62010 106744 63657 0 58.1   66 0 0 0 -2497  -82  .19999695  .09999847
    "1953-04-01" 1723 50435 2.7 61444 106826 63167 0 57.5 65.5 0 0 0 -2466  -81        -.5  -.5999985
    "1953-05-01" 1596 50490 2.5 61019 106910 62615 0 57.1 64.9 1 0 0 -2436  -80  -.5999985  -.4000015
    "1953-06-01" 1607 50519 2.5 61456 106978 63063 0 57.4 65.3 0 0 1 -2405  -79   .4000015  .30000305
    "1953-07-01" 1660 50536 2.6 61397 107034 63057 0 57.4 65.4 0 0 1 -2375  -78  .09999847          0
    "1953-08-01" 1665 50489 2.7 61151 107132 62816 1 57.1 65.2 0 0 1 -2344  -77 -.20000458 -.30000305
    "1953-09-01" 1821 50368 2.9 60906 107253 62727 1 56.8   65 0 0 1 -2313  -76 -.19999695  -.2999992
    "1953-10-01" 1974 50240 3.1 60893 107383 62867 1 56.7 65.1 0 0 1 -2283  -75  .09999847 -.09999847
    "1953-11-01" 2211 49908 3.5 60738 107504 62949 1 56.5 65.3 0 0 1 -2252  -74  .20000458 -.20000076
    "1953-12-01" 2818 49703 4.5 59977 107623 62795 1 55.7 64.6 0 0 1 -2222  -73  -.7000046  -.7999992
    "1954-01-01" 3077 49469 4.9 60024 107763 63101 1 55.7 64.2 0 0 1 -2191  -72  -.4000015          0
    "1954-02-01" 3331 49382 5.2 60663 107880 63994 1 56.2 64.6 0 0 1 -2160  -71   .4000015         .5
    "1954-03-01" 3607 49157 5.7 60186 107987 63793 1 55.7   64 0 0 1 -2132  -70  -.5999985        -.5
    "1954-04-01" 3749 49179 5.9 60185 108080 63934 1 55.7   64 0 0 1 -2101  -69          0          0
    "1954-05-01" 3767 48965 5.9 59908 108184 63675 1 55.4 63.7 0 0 1 -2071  -68  -.2999992  -.2999992
    "1954-06-01" 3551 48895 5.6 59792 108267 63343 0 55.2 63.6 0 0 1 -2040  -67 -.10000229 -.20000076
    "1954-07-01" 3659 48835 5.8 59643 108344 63302 0   55 63.4 0 0 1 -2010  -66 -.19999695 -.20000076
    "1954-08-01" 3854 48826   6 59853 108440 63707 0 55.2 63.4 0 0 1 -1979  -65          0  .20000076
    "1954-09-01" 3927 48886 6.1 60282 108546 64209 0 55.5 63.7 0 1 1 -1948  -64   .2999992   .2999992
    "1954-10-01" 3666 48942 5.7 60270 108668 63936 0 55.5 63.6 0 0 0 -1918  -63 -.10000229          0
    "1954-11-01" 3402 49180 5.3 60357 108798 63759 0 55.5 64.1 0 0 0 -1887  -62         .5          0
    "1954-12-01" 3196 49331   5 60116 108892 63312 0 55.2   64 0 0 0 -1857  -61 -.09999847  -.2999992
    "1955-01-01" 3157 49496 4.9 60753 109059 63910 0 55.7 64.5 0 0 0 -1826  -60         .5         .5
    "1955-02-01" 2969 49644 4.7 60727 109078 63696 0 55.7 64.5 0 0 0 -1795  -59          0          0
    "1955-03-01" 2918 49962 4.6 60964 109254 63882 0 55.8 64.4 0 0 0 -1767  -58 -.09999847  .09999847
    "1955-04-01" 3049 50248 4.7 61515 109377 64564 0 56.2 64.8 0 0 0 -1736  -57   .4000015   .4000015
    "1955-05-01" 2747 50512 4.3 61634 109544 64381 0 56.3   65 0 0 0 -1706  -56  .19999695  .09999847
    "1955-06-01" 2701 50790 4.2 61781 109680 64482 0 56.3 65.3 0 0 0 -1675  -55  .30000305          0
    "1955-07-01" 2632 50987   4 62513 109792 65145 0 56.9 65.7 0 0 0 -1645  -54   .3999939   .6000023
    "1955-08-01" 2784 51111 4.2 62797 109882 65581 0 57.1 65.8 0 0 0 -1614  -53   .1000061  .19999695
    "1955-09-01" 2678 51266 4.1 62950 109977 65628 0 57.2 65.7 0 0 0 -1583  -52  -.1000061  .10000229
    "1955-10-01" 2830 51429 4.3 62991 110085 65821 0 57.2 65.6 0 0 0 -1553  -51 -.09999847          0
    "1955-11-01" 2780 51592 4.2 63257 110177 66037 0 57.4 65.6 0 0 0 -1522  -50          0  .20000076
    "1955-12-01" 2761 51805 4.2 63684 110296 66445 0 57.7 65.9 0 0 0 -1492  -49  .30000305   .2999992
    "1956-01-01" 2666 51975   4 63753 110390 66419 0 57.8 66.2 0 0 0 -1461  -48   .2999954  .09999847
    "1956-02-01" 2606 52167 3.9 63518 110478 66124 0 57.5 65.8 0 0 0 -1430  -47  -.3999939  -.2999992
    "1956-03-01" 2764 52294 4.2 63411 110582 66175 0 57.3 65.7 0 0 0 -1401  -46  -.1000061 -.20000076
    "1956-04-01" 2650 52375   4 63614 110650 66264 0 57.5 65.9 0 0 0 -1370  -45  .20000458  .20000076
    end
    format %td date
    format %tm datem
    I am trying to make a local macro for certain months:
    Code:
    local peaks 1948m1 1953m5 1957m3 1960m2 1968m9 1973m10
    And I am trying to see if I can include it in my tsline chart:

    Code:
    twoway (tsline empop, lcolor(blue)), tline(`peaks') ytitle("Employment to Population Rate") xtitle("") tlabel(, format(%tm))
    Unfortunately, when I run it, it gives me this instead:

    Click image for larger version

Name:	Example_chart.PNG
Views:	1
Size:	61.9 KB
ID:	1596862


    The tsline of the respective months seem to be missing. Is there something I missed in my coding? Or is it the case that macros cannot be used together with tsline command?

    Any help in this area would be appreciated. Thanks!





  • #2
    You do not show your tsset command. There does not seem to be anything wrong with your code. Adding

    Code:
    tsset datem
    this is what I get:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	92.2 KB
ID:	1596923

    Last edited by Andrew Musau; 09 Mar 2021, 07:10.

    Comment


    • #3

      Your results are consistent with the local macro peaks being undefined at the time the tsline command is run.

      I believe your problem is that you have written your code in the do-file editor window, and then rather than running everything at once, you are running it by selecting the local command and running it, then selecting the tsline command and running it, rather than running the two together.

      Consider the following example. In the do-file editor window, I have a two-line program that I run in its entirety.
      Code:
      . do "/Users/lisowskiw/Downloads/example.do"
      
      . local message Hello, world.
      
      . display "The message is `message'"
      The message is Hello, world.
      
      . 
      end of do-file
      Now I run the same two lines by selecting the first line and running it, then selecting the second line and running it.
      Code:
      . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
      
      . local message Hello, world.
      
      . 
      end of do-file
      
      . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
      
      . display "The message is `message'"
      The message is 
      
      . 
      end of do-file
      The important thing to keep in mind is that local macros vanish when the do-file within which they were created ends. If you look carefully at the results above, you'll see that when I selected a single line to run, it was copied into a temporary do-file and run, so even though both lines are in the same window in the do-file editor, they are run as separate do-files, and local macro defined in the first line vanishes at the end of that do-file, and is undefined when the second line is run.

      Comment


      • #4
        Thanks for the help, it works well!

        As a follow-up question regarding macros, I would like to do several things with it:

        1) Is it possible to create a first macro that would extract the month before usrec =1 and months when usrec = 1? I need the month before usrec = 1. So for example, if 1948m12 is the first month when usrec =1, I want to extract 1948m11. I would also need the macro to extract the months when usrec =1.


        2) I need a second macro that extracts the month when peaks =1 and the month when troughs = 1. Ideally, it would find the month of the peak first, followed by a trough, then back to peak and so on. This would continue until the end of the dataset.


        I am currently using the following macros for the charts:

        Code:
        local peakstroughs 1948m1 1949m10 1953m5 1954m9 1957m3 1958m7 1960m2 1961m5 1968m9 1970m12 1973m10 1975m5 1979m5 1982m11 1989m3 1992m6 2000m4 2003m6 2006m10 2009m10 2019m9 2020m4 2021m1  // vertical lines macros for peaks and troughs
        
        local months 1948m11 1948m12    1949m1    1949m2    1949m3    1949m4    1949m5    1949m6    1949m7    1949m8    1949m9    1949m10 1953m7    1953m8    1953m9    1953m10    1953m11    1953m12    1954m1    1954m2    1954m3    1954m4    1954m5 1957m8    1957m9    1957m10 1957m11    1957m12    1958m1    1958m2    1958m3    1958m4 1960m4    1960m5    1960m6    1960m7    1960m8    1960m9    1960m10    1960m11    1960m12    1961m1    1961m2 1969m12    1970m1    1970m2    1970m3    1970m4    1970m5    1970m6    1970m7    1970m8    1970m9    1970m10    1970m11 1973m11    1973m12    1974m1    1974m2    1974m3    1974m4    1974m5    1974m6    1974m7    1974m8    1974m9    1974m10    1974m11    1974m12    1975m1    1975m2    1975m3 1980m1    1980m2    1980m3    1980m4    1980m5    1980m6    1980m7 1981m7    1981m8    1981m9    1981m10    1981m11    1981m12    1982m1    1982m2    1982m3    1982m4    1982m5    1982m6    1982m7    1982m8 1982m9    1982m10    1982m11 1990m7    1990m8    1990m9    1990m10    1990m11    1990m12    1991m1    1991m2    1991m3 2001m3    2001m4    2001m5    2001m6    2001m7    2001m8    2001m9    2001m10    2001m11 2007m12    2008m1    2008m2    2008m3    2008m4    2008m5    2008m6    2008m7    2008m8    2008m9    2008m10    2008m11    2008m12    2009m1    2009m2    2009m3    2009m4    2009m5    2009m6 2020m2    2020m3    2020m4    2020m5    2020m6    2020m7    2020m8    2020m9    2020m10    2020m11    2020m12    2021m1
        // shaded area
        
        twoway (tsline empop, lcolor(blue)), ///
                tline(`months',lwidth(thick) lcolor(gs12)) ///
                tline(`peakstroughs', lcolor(gs12) lpattern(straight)) ytitle("Employment to Population Rate") xtitle("") tlabel(, format(%tmCCYY))

        As you can see, the list of months are long so I am looking for a more elegant solution.

        Any help in this area would be appreciated. Thanks!




        Comment


        • #5
          Without getting into micro-level coding, the solution probably lies with the levelsof command. As long as you have identified peaks and troughs, you can do something like:

          Code:
          egen tempvar= rowmax(peaks troughs)
          levelsof datem if tempvar, local(peakstroughs)
          drop tempvar

          Order of months in the local macro does not matter here.
          Last edited by Andrew Musau; 10 Mar 2021, 04:34.

          Comment

          Working...
          X