Hi,

I am using Stata 16 and would need some help in programming. I have a matlab script and would need to see if I could create Stata coding that is similar in function when compared to the Matlab script. Below is my matlab code:

Code:
function [peaks,troughs] = findpeaksandtroughs(u,size) %% find peak, just key in peak and vice versa
% - u is the (e.g. unemployment) time series under investigation
% - size is the minimal size of a swing to qualify as a peak or trough
% - peaks is a dummy time series that indicate identified peaks with 1
% - troughs is a dummy time series that indicate identified troughs with 1
% Note: As the function is initially intended for unemployment time series, a
% "peak" here is a low value of u
% (c) Stephane Dupraz, Emi Nakamura, Jon Steinsson 2019

T           =length(u);
peaks       =zeros(T,1);
troughs     =zeros(T,1);

% Initial Conditions
candidate     =1;%t=1 is our first candidate for a peak or a trough
if u(1)<u(2)
    lookingfor='peak';%t=1 is a candidate for a peak
else
    lookingfor='trough';%t=1 is a candidate for a trough
end

for t=2:T
   if strcmp(lookingfor,'peak')% if we are looking for a peak
        if u(t)<u(candidate)
            candidate=t;% our candidate for a peak becomes t
        else % u(t)>=u(candidate)
        if u(t)>=u(candidate)+size % u has increased by more than x since our candidate for peak
            peaks(candidate)=1; %our candidate is a peak indeed
            lookingfor        ='trough';% we now look for a trough
            candidate         =t;% t is the first candidate for such a trough
        end
        end
   end
   if strcmp(lookingfor,'trough')% if we are looking for a trough
        if u(t)>u(candidate)
            candidate=t;% our candidate for a trough becomes t
        else % u(t)<=u(candidate)
        if u(t)<=u(candidate)-size % u has decreased by more than x since our candidate for trough
            troughs(candidate)=1; %our candidate is a trough indeed
            lookingfor        ='peak';% we now look for a peak
            candidate         =t;% t is the first candidate for such a peak
        end
        end
   end       
end

% Final call
if strcmp(lookingfor,'trough')
   troughs(candidate)=1;
end
if strcmp(lookingfor,'peak')
   peaks(candidate)=1;
end

end

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

I am not sure if Stata can run matlab codes or other programming languages codes. If I can't run Matlab codes, I would have to create Stata codes that have the same function as the Matlab codes but I am unsure whether Stata can create and run functions just like Matlab.

Any help in this area is appreciated. Thanks!