The wild broccoli 🥦

Current mood: feeling The current mood of oleracea at www.imood.com

Much ado about nothing

Mood: 👩‍💻 codeful


For work, I mostly use Stata and a little bit of R and Mplus. I’ve been learning SQL and SAS in school as well, which is pretty conceptually similar to Stata. However, the SAS syntax is no fun for me, so I will continue to appreciate that my job lets me use Stata instead.

The ado-files that I write are largely tiny utilities that define macros that I like to use over and over again. For example, in today.ado, I define a formatted date variable that I can use to name versions of files:

1program today
2	global today : di %tdN.D.CY daily("$S_DATE", "DMY")
3
4	di "Today is $today"
5end

It also displays today’s date upon initializing, which is useful for double checking. Usually I use this when creating Excel files, like this:

1today
2
3putexcel set table.$today.xlsx, open modify sheet("Table 1",replace)
4/* The rest of my code goes here */
5putexcel save

Another ado-file that is extremely useful when generating an Excel table is the aptly named excelfills.ado. This file just defines the RGB values of Excel’s “good”, “bad”, and “neutral” formats, which I can use to automatically highlight certain results.

1program excelfills
2	global GOODTEXT = "000 097 000"
3	global GOODFILL = "198 239 206"
4	global MEDTEXT  = "156 087 000"
5	global MEDFILL  = "255 235 156"
6	global BADTEXT  = "156 000 006"
7	global BADFILL  = "255 199 206"
8	di "Good text and fill:  $GOODTEXT $GOODFILL"
9end

So for example, I could highlight a statistically significant p-value by writing:

 1today
 2excelfills
 3
 4sysuse auto
 5
 6putexcel set table.$today.xlsx, open modify sheet("Bivariate regression",replace)
 7
 8putexcel A1 = "Variable" B1 = "Coefficient" C1 = "SE" D1 = "p"
 9
10regress price mpg i.foreign
11local b_mpg = _b[mpg]
12local b_foreign = _b[1.foreign]
13local se_mpg = _se[mpg]
14local se_foreign = _se[1.foreign]
15putexcel A2 = "`:var l mpg'" 
16putexcel B2 = `b_mpg',nformat(0.00) 
17putexcel C2 = `se_mpg',nformat((0.00)
18
19test mpg
20local p = r(p)
21if `p' < 0.05 {
22	putexcel D2 = `p',nformat(0.000) font(,,$GOODTEXT) fpattern(solid,$GOODFILL)
23	if `p' < 0.001 {
24		putexcel D2 = "<0.001"
25	}
26}
27else {
28	putexcel D2 = `p',nformat(0,000)
29}
30
31putexcel A3 = "`:var l foreign'" 
32putexcel B3 = `b_foreign',nformat(0.00)
33putexcel C3 = `se_foreign',nformat((0.00))
34
35testparm i.foreign
36local p = r(p)
37if `p' < 0.05 {
38	putexcel D2 = `p',nformat(0.000) font(,,$GOODTEXT) fpattern(solid,$GOODFILL)
39	if `p' < 0.001 {
40		putexcel D2 = "<0.001"
41	}
42}
43else {
44	putexcel D2 = `p',nformat(0,000)
45}
46
47putexcel save

Normally I would actually iterate over the variables in a loop and increment the row using another macro, but this is a simple example.


A lot of the time, I end up running a more complicated model than a linear regression. And these sorts of models can take a really long time to converge. I don’t want to spend all my time staring at the screen waiting for my output, so I downloaded the Windows 3.1x “tada.wav” startup sound1 and put it inside my \ado\personal folder. I then wrote the following little ado-file (tada.ado) to catch my attention when my regression finishes running:

1program tada 
2
3	qui findfile tada.wav
4	global S_SHELL "PowerShell.exe -WindowStyle Minimized"
5	
6	! (New-Object Media.SoundPlayer "`r(fn)'").PlaySync();
7
8end

This calls Powershell to play my sound. I put it after the regression command, like this:

1mi estimate: mixed y1 x1 x2 x3 x4 i.x5 || id:
2tada

You can actually use any sound you want. I suggest using a very short sound though.


Due to my health issues recently I have felt more inclined to pray. However, I do not have a lot of free time to dedicate to prayer (I am a bad Catholic in this regard). Therefore, I wrote a little program to have Stata pray for me. It automatically detects the day of the week so that you can contemplate the appropriate Mystery, and also detects certain holidays. This is a pretty simple and ridiculous program, but I do have fun running it before I start working.

  1program define nextline
  2	set more on
  3	more
  4	set more off
  5end
  6
  7program define sign_of_cross
  8	di as text "In the name of the Father, and the Son, and the Holy Spirit."
  9	nextline
 10	di as result "Amen."
 11	di
 12end
 13
 14program define apostles
 15	di as text "I believe in God," _n "the Father almighty," _n "Creator of heaven and earth," _n "and in Jesus Christ, his only Son, our Lord," _n "who was conceived by the Holy Spirit," _n "born of the Virgin Mary," _n "suffered under Pontius Pilate," _n "was crucified, died, and was buried;" _n "he descended into hell;" _n "on the third day he rose again from the dead;" _n "he ascended into heaven," _n "and is seated at the right hand of God the Father almighty;" _n "from there he will come to judge the living and the dead."
 16	di
 17	nextline
 18	di as text "I believe in the Holy Spirit," _n "the holy catholic Church," _n "the communion of saints," _n "the forgiveness of sins," _n "the resurrection of the body," _n "and life everlasting."
 19	di
 20	nextline
 21	di as result "Amen."
 22	di
 23end
 24
 25program define our_father
 26	di as text "Our Father, who art in heaven," _n "hallowed be thy name;" _n "thy kingdom come," _n "thy will be done" _n "on earth as it is in heaven."
 27	nextline
 28	di as text "Give us this day our daily bread,"_n "and forgive us our trespasses," _n "as we forgive those who trespass against us;" _n "and lead us not into temptation," _n "but deliver us from evil."
 29	di
 30	nextline
 31	di as result "Amen."
 32	di
 33end
 34
 35program define hail_mary
 36	di as text "Hail, Mary, full of grace," _n "the Lord is with thee." _n "Blessed are thou among women" _n "and blessed is the fruit of thy womb, Jesus."
 37	nextline
 38	di as text "Holy Mary, Mother of God," _n "pray for us sinners," _n "now and at the hour of our death."
 39	di
 40	nextline
 41	di as result "Amen."
 42	di
 43end
 44
 45program define glory_be
 46	di as text "Glory be to the Father" _n "and to the Son" _n "and to the Holy Spirit,"
 47	nextline
 48	di as text "as it was in the beginning" _n "is now" _n "and ever shall be" _n "world without end."
 49	di
 50	nextline
 51	di as result "Amen."
 52	di
 53end
 54
 55program define o_my_jesus
 56	di as text "O my Jesus," _n "forgive us our sins," _n "save us from the fires of hell;"
 57	nextline
 58	di as text "lead all souls to Heaven," _n "especially those who have most need of your mercy."
 59	di
 60end
 61
 62program define salve_regina
 63	di as text "Hail, Holy Queen, Mother of Mercy" _n "our life, our sweetness and our hope." _n "To thee do we cry," _n "poor banished children of Eve." _n "To thee do we send up our sighs," _n "mourning and weeping in this valley of tears."
 64	nextline
 65	di as text "Turn then, most gracious advocate," _n "thing eyes of mercy toward us," _n "and after this our exile" _n "show unto us the blessed fruit of thy womb, Jesus."
 66	nextline 
 67	di as text "O clement, O loving," _n "O sweet Virgin Mary."
 68	di
 69end
 70
 71program define saint_joseph
 72	di as text "To you, O blessed Joseph," _n "do we come to you in our tribulation," _n "and having implored the help of your most holy Spouse," _n "we confidently invoke your patronage also."
 73	display
 74	nextline
 75	di as text "Through that charity which bound you" _n "to the Immaculate Virgin Mother of God" _n "and through the paternal love" _n "with which you embraced the Child Jesus," _n "we humbly beg you graciously to regard the inheritance" _n "which Jesus Christ has purchased by his Blood," _n "and with your power and strength to aid us in our necessities."
 76	display
 77	nextline
 78	di as text "O most watchful guardian of the Holy Family," _n "defend the chosen children of Jesus Christ;" _n "O most loving father, ward off from us" _n "every contagion of error and corrupting influence" _n "O our most mighty protector, be kind to us" _n "and from heaven assist us in our struggle" _n "with the power of darkness."
 79	display
 80	nextline
 81	di as text "As once you rescued the Child Jesus from deadly peril," _n "so now protect God's Holy Church" _n "from the snares of the enemy and from all adversity" _n "shield, too, each one of us by your constant protection," _n "so that, supported by your example and your aid," _n "we may be able to live piously, to die in holiness," _n "and to obtain eternal happiness in heaven."
 82	display
 83	nextline
 84	di as result "Amen."
 85	display
 86end
 87
 88program define rosary
 89	
 90	local weekday = dow(date(c(current_date),"DMY"))
 91	
 92	local daily = date(c(current_date),"DMY")
 93	
 94	local y = yofd(`daily')
 95	local a = mod(`y',19)
 96	local b = floor(`y'/100)
 97	local c = mod(`y',100)
 98	local d = floor(`b'/4)
 99	local e = mod(`b',4)
100	local f = floor((`b' + 8)/25)
101	local g = floor((`b' - `f' + 1)/3)
102	local h = mod(((19 * `a') + `b' - `d' - `g' + 15),30)
103	local i = floor(`c'/4)
104	local k = mod(`c',4)
105	local l = mod((32 + (2 * `e') + (2 * `i') - `h' - `k'),7)
106	local m = floor((`a' + (11 * `h') + (22 * `l'))/(451))
107	local n = floor((`h' + `l' - (7 * `m') + 114)/31)
108	local o = mod((`h' + `l' - (7 * `m') + 114),31) + 1
109	
110	local easter = date("`n'/`o'/`y'","MDY")
111	local lent = `easter' - 46
112	local lentflag = `daily' <= `=`easter'-3' & `daily' >= `lent'
113	
114	local christmas = date("12/25/`y'","MDY")
115	local advent = date("12/3/`y'","MDY") - day(date("12/3/`y'","MDY"))
116	local christmasflag = `daily' < `christmas' & `daily' >= `advent'
117	
118	
119	//Monday and Saturday (Advent Sunday TBA)
120	local mystery  "The joyful mysteries"
121	local mystery1 "The Annunciation"
122	local mystery2 "The Visitation"
123	local mystery3 "The Nativity"
124	local mystery4 "The Presentation in the Temple"
125	local mystery5 "The Finding in the Temple"
126	
127	local reading1 "In the sixth month, the angel Gabriel was sent from God to a town of Galilee called Nazareth, to a virgin betrothed to a man named Joseph, of the house of David, and the virgin's name was Mary."
128	//di as text "`reading1'"
129	local reading2 "During those days Mary set out and traveled to the hill country in haste to a town of Judah, where she entered the house of Zechariah and greeted Elizabeth. When Elizabeth heard Mary's greeting, the infant leaped in her womb, and Elizabeth, filled with the holy Spirit, cried out in a loud voice and said, 'Most blessed are you among women, and blessed is the fruit of your womb.'"
130	local reading3 "In those days a decree went out from Caesar Augustus that the whole world should be enrolled. This was the first enrollment, when Quirinius was governor of Syria. So all went to be enrolled, each to his own town. And Joseph too went up from Galilee from the town of Nazareth to Judea, to the city of David that is called Bethlehem, because he was of the house and family of David, to be enrolled with Mary, his betrothed, who was with child. While they were there, the time came for her to have her child, and she gave birth to her firstborn son. She wrapped him in swaddling clothes and laid him in a manger, because there was no room for them in the inn."
131	local reading4 "When eight days were completed for his circumcision, he was named Jesus, the name given him by the angel before he was conceived in the womb." _n "When the days were completed for their purification according to the law of Moses, they took him up to Jerusalem to present him to the Lord, just as it is written in the law of the Lord, 'Every male that opens the womb shall be consecrated to the Lord,' and to offer the sacrifice of 'a pair of turtledoves or two young pigeons,' in accordance with the dictate in the law of the Lord."
132	local reading5 "Each year his parents went to Jerusalem for the feast of Passover,  and when he was twelve years old, they went up according to festival custom. After they had completed its days, as they were returning, the boy Jesus remained behind in Jerusalem, but his parents did not know it. Thinking that he was in the caravan, they journeyed for a day and looked for him among their relatives and acquaintances, but not finding him, they returned to Jerusalem to look for him. After three days they found him in the temple, sitting in the midst of the teachers, listening to them and asking them questions, and all who heard him were astounded at his understanding and his answers."
133	
134	local fruit1 "Humility"
135	local fruit2 "Love of Neighbor"
136	local fruit3 "Poverty"
137	local fruit4 "Purity of Heart and Body"
138	local fruit5 "Devotion to Jesus"
139	
140	
141	// Tuesday and Friday (Lenten Sunday TBA)
142	if `weekday' == 2 | `weekday' == 5 | (`weekday' == 0 & `lentflag' == 1) {
143		local mystery "The sorrowful mysteries"
144		local mystery1 "The Agony in the Garden"
145		local mystery2 "The Scourging at the Pillar"
146		local mystery3 "The Crowning with Thorns"
147		local mystery4 "The Carrying of the Cross"
148		local mystery5 "The Crucifixion and Death"
149		
150		local reading1 "Then Jesus came with them to a place called Gethsemane, and he said to his disciples, 'Sit here while I go over there and pray.' He took along Peter and the two sons of Zebedee, and began to feel sorrow and distress. Then he said to them, 'My soul is sorrowful even to death. Remain here and keep watch with me.' He advanced a little and fell prostrate in prayer, saying, 'My Father, if it is possible, let this cup pass from me; yet, not as I will, but as you will.'"
151		local reading2 "Then he released Barabbas to them, but after he had Jesus scourged, he handed him over to be crucified."
152		local reading3 "Then the soldiers of the governor took Jesus inside the praetorium and gathered the whole cohort around him. They stripped off his clothes and threw a scarlet military cloak about him. Weaving a crown out of thorns, they placed it on his head, and a reed in his right hand. And kneeling before him, they mocked him, saying, 'Hail, King of the Jews!'"
153		local reading4 "They pressed into service a passer-by, Simon, a Cyrenian, who was coming in from the country, the father of Alexander and Rufus, to carry his cross. They brought him to the place of Golgotha."
154		local reading5 "When they came to the place called the Skull, they crucified him and the criminals there, one on his right, the other on his left. Then Jesus said, 'Father, forgive them, they know not what they do.' They divided his garments by casting lots. The people stood by and watched; the rulers, meanwhile, sneered at him and said, 'He saved others, let him save himself if he is the chosen one, the Messiah of God.' Even the soldiers jeered at him. As they approached to offer him wine they called out, 'If you are King of the Jews, save yourself.' Above him there was an inscription that read, 'This is the King of the Jews.' Now one of the criminals hanging there reviled Jesus, saying, 'Are you not the Messiah? Save yourself and us.' The other, however, rebuking him, said in reply, 'Have you no fear of God, for you are subject to the same condemnation? And indeed, we have been condemned justly, for the sentence we received corresponds to our crimes, but this man has done nothing criminal.' Then he said, 'Jesus, remember me when you come into your kingdom.' He replied to him, 'Amen, I say to you, today you will be with me in Paradise.'" _n "It was now about noon and darkness came over the whole land until three in the afternoon because of an eclipse of the sun. Then the veil of the temple was torn down the middle. Jesus cried out in a loud voice, 'Father, into your hands I commend my spirit'; and when he had said this he breathed his last."
155		
156		local fruit1 "Obedience to God's Will"
157		local fruit2 "Mortification"
158		local fruit3 "Courage"
159		local fruit4 "Patience"
160		local fruit5 "Sorrow for our Sins"
161	}
162	
163	// Wednesday and Ordinary Sunday
164	if `weekday' == 0 | `weekday' == 3 {
165		local mystery "The glorious mysteries"
166		local mystery1 "The Resurrection"
167		local mystery2 "The Ascension"
168		local mystery3 "The Descent of the Holy Spirit"
169		local mystery4 "The Assumption"
170		local mystery5 "The Coronation of Mary"
171		
172		local reading1 "But at daybreak on the first day of the week they took the spices they had prepared and went to the tomb.They found the stone rolled away from the tomb; but when they entered, they did not find the body of the Lord Jesus. While they were puzzling over this, behold, two men in dazzling garments appeared to them. They were terrified and bowed their faces to the ground. They said to them, 'Why do you seek the living one among the dead? He is not here, but he has been raised.'"
173		local reading2 "So then the Lord Jesus, after he spoke to them, was taken up into heaven and took his seat at the right hand of God."
174		local reading3 "When the time for Pentecost was fulfilled, they were all in one place together. And suddenly there came from the sky a noise like a strong driving wind, and it filled the entire house in which they were. Then there appeared to them tongues as of fire, which parted and came to rest on each one of them. And they were all filled with the holy Spirit and began to speak in different tongues, as the Spirit enabled them to proclaim."
175		local reading4 "Behold, from now on will all ages call me blessed." _n "The Mighty One has done great things for me," _n "and holy is his name."
176		local reading5 "A great sign appeared in the sky, a woman clothed with the sun, with the moon under her feet, and on her head a crown of twelve stars."
177		
178		local fruit1 "Faith"
179		local fruit2 "Hope"
180		local fruit3 "Wisdom"
181		local fruit4 "Devotion to Mary"
182		local fruit5 "Grace of a happy death"
183	}
184	
185	// Thursday
186	if `weekday' == 4 {
187		local mystery "The luminous mysteries"
188		local mystery1 "The Baptism of Christ in the Jordan"
189		local mystery2 "The Wedding Feast at Cana"
190		local mystery3 "Jesus' Proclamation of the Coming of the Kingdom of God"
191		local mystery4 "The Transfiguration"
192		local mystery5 "The Institution of the Eucharist"
193		
194		local reading1 "After Jesus was baptized, he came up from the water and behold, the heavens were opened for him, and he saw the Spirit of God descending like a dove and coming upon him. And a voice came from the heavens, saying, 'This is my beloved Son, with whom I am well pleased.'"
195		local reading2 "On the third day there was a wedding in Cana in Galilee, and the mother of Jesus was there. Jesus and his disciples were also invited to the wedding. When the wine ran short, the mother of Jesus said to him, 'They have no wine.' [And] Jesus said to her, 'Woman, how does your concern affect me? My hour has not yet come.' His mother said to the servers, 'Do whatever he tells you."
196		local reading3 "'This is the time of fulfillment. The kingdom of God is at hand. Repent, and believe in the gospel.'"
197		local reading4 "After six days Jesus took Peter, James, and John his brother, and led them up a high mountain by themselves. And he was transfigured before them; his face shone like the sun and his clothes became white as light."
198		local reading5 "While they were eating, Jesus took bread, said the blessing, broke it, and giving it to his disciples said, 'Take and eat; this is my body.'"
199		
200		local fruit1 "Openness to the Holy Spirit"
201		local fruit2 "To Jesus through Mary"
202		local fruit3 "Conversion"
203		local fruit4 "Desire for holiness"
204		local fruit5 "Adoration"
205	}
206	
207	if `weekday' == 0 & `christmasflag' == 1 {
208		local mystery  "The joyful mysteries"
209		local mystery1 "The Annunciation"
210		local mystery2 "The Visitation"
211		local mystery3 "The Nativity"
212		local mystery4 "The Presentation in the Temple"
213		local mystery5 "The Finding in the Temple"
214		
215		local reading1 "In the sixth month, the angel Gabriel was sent from God to a town of Galilee called Nazareth, to a virgin betrothed to a man named Joseph, of the house of David, and the virgin's name was Mary."
216		//di as text "`reading1'"
217		local reading2 "During those days Mary set out and traveled to the hill country in haste to a town of Judah, where she entered the house of Zechariah and greeted Elizabeth. When Elizabeth heard Mary's greeting, the infant leaped in her womb, and Elizabeth, filled with the holy Spirit, cried out in a loud voice and said, 'Most blessed are you among women, and blessed is the fruit of your womb.'"
218		local reading3 "In those days a decree went out from Caesar Augustus that the whole world should be enrolled. This was the first enrollment, when Quirinius was governor of Syria. So all went to be enrolled, each to his own town. And Joseph too went up from Galilee from the town of Nazareth to Judea, to the city of David that is called Bethlehem, because he was of the house and family of David, to be enrolled with Mary, his betrothed, who was with child. While they were there, the time came for her to have her child, and she gave birth to her firstborn son. She wrapped him in swaddling clothes and laid him in a manger, because there was no room for them in the inn."
219		local reading4 "When eight days were completed for his circumcision, he was named Jesus, the name given him by the angel before he was conceived in the womb." _n "When the days were completed for their purification according to the law of Moses, they took him up to Jerusalem to present him to the Lord, just as it is written in the law of the Lord, 'Every male that opens the womb shall be consecrated to the Lord,' and to offer the sacrifice of 'a pair of turtledoves or two young pigeons,' in accordance with the dictate in the law of the Lord."
220		local reading5 "Each year his parents went to Jerusalem for the feast of Passover,  and when he was twelve years old, they went up according to festival custom. After they had completed its days, as they were returning, the boy Jesus remained behind in Jerusalem, but his parents did not know it. Thinking that he was in the caravan, they journeyed for a day and looked for him among their relatives and acquaintances, but not finding him, they returned to Jerusalem to look for him. After three days they found him in the temple, sitting in the midst of the teachers, listening to them and asking them questions, and all who heard him were astounded at his understanding and his answers."
221		
222		local fruit1 "Humility"
223		local fruit2 "Love of Neighbor"
224		local fruit3 "Poverty"
225		local fruit4 "Purity of Heart and Body"
226		local fruit5 "Devotion to Jesus"
227	}
228	
229	sign_of_cross
230	nextline
231	apostles
232	nextline
233	our_father
234	nextline
235	forv m = 1/3 {
236		hail_mary
237		nextline
238	}
239	glory_be
240	nextline
241	
242	di as result "{bf:`mystery'}"
243	nextline
244	di
245	forv i = 1/5 {
246		di as result "{bf:`mystery`i''}"
247
248		di as text "`reading`i''"
249		di as text "Fruit of the mystery: {bf:`fruit`i''}"
250		nextline
251		di
252		our_father
253		forv m = 1/10 {
254			hail_mary
255			nextline
256		}
257		glory_be
258		nextline
259		o_my_jesus
260		nextline
261		
262	}
263	
264	salve_regina
265	nextline
266	di as text "Pray for us, O holy Mother of God."
267	nextline
268	di as text "That we may be made worthy of the promises of Christ."
269	di
270	nextline
271	di as result "Let us pray: " 
272	nextline
273	di as text "O God, whose Only Begotten Son, by his life, Death, and Resurrection, has purchased for us the rewards of eternal life, grant, we beseech thee, that while meditating on these mysteries of the most holy Rosary of the Blessed Virgin Mary, we may imitate what they contain and obtain what they promise, through the same Christ our Lord."
274	di _n as result "Amen."
275	di
276	nextline
277	saint_joseph
278	nextline
279	
280	
281	sign_of_cross
282	set more off
283end

This is pretty easy to run once you have the ado-file in your personal directory. Just type rosary and Stata will pray the rosary for you!


Sorry that there is no syntax highlighting. Stata doesn’t seem to be supported in Hugo’s markdown editor. I doubt you will need to use any of these ado-files in your everyday life, but if you ever do, please let me know! I will appreciate it 😉. One of these days I’ll write something actually useful too.


  1. A history of Windows startup sounds. I really like the Windows 95 startup sound, which was composed by Brian Eno. ↩︎

#Blog #Code