-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwa_stata_training_201812.do
1867 lines (1615 loc) · 59 KB
/
rwa_stata_training_201812.do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
********************************************************************************
* STATA WORKSHOP - DECEMBER 2018
*
* Created by: Roshni Khincha
********************************************************************************
********************************************************************************
* Set up
********************************************************************************
clear
* User identification
if "`c(username)'" == "roshn" {
global dropbox "C:\Users\roshn\Dropbox"
global github "C:\Users\roshn\Documents\GitHub"
}
if "`c(username)'" == "WB528092" {
global dropbox "C:\Users\WB528092\Dropbox"
global github "C:\Users\WB528092\Documents\GitHub"
}
* File path
global data "$dropbox\minagri_stata_training_aug2018\data"
* packages
global install 0 // Turn to 1 here if you want to install
if $install == 1 {
ssc install texdoc, replace
net from http://www.stata-journal.com/production
net install sjlatex, replace
}
********************************************************************************
* Write the tex file
********************************************************************************
* Initiate the latex doc
texdoc init "$script/rwa_stata_training_201812", replace ///
gropts(optargs(width=0.8\textwidth))
/***
% Set document type and scheme
\documentclass[10pt]{beamer}
\usetheme[progressbar=frametitle]{metropolis}
% Load packages
\usepackage{appendixnumberbeamer}
\usepackage{booktabs}
\usepackage[scale=2]{ccicons}
\usepackage{pgfplots}
\usepackage{xspace}
\newcommand{\themename}{\textbf{\textsc{metropolis}}\xspace}
\usepackage{stata}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{hyperref}
\hypersetup{colorlinks=true,
linkcolor=black,
urlcolor=blue,}
\title{Stata Workshop} %% that will be typeset on the
\subtitle{} %% title page.
\author{Roshni Khincha \\ DIME, World Bank}
\date{December, 2018}
\titlegraphic{%
\includegraphics[width=.2\textwidth]{DIME}\hfill % I think I should probably ask for a better image for this thing....
\includegraphics[width=.15\textwidth]{logo_minagri}\hfill
\includegraphics[width=.2\textwidth]{logo_eu}
}
\makeatletter
\setbeamertemplate{title page}{
\begin{minipage}[b][\paperheight]{\textwidth}
\vfill%
\ifx\inserttitle\@empty\else\usebeamertemplate*{title}\fi
\ifx\insertsubtitle\@empty\else\usebeamertemplate*{subtitle}\fi
\usebeamertemplate*{title separator}
\ifx\beamer@shortauthor\@empty\else\usebeamertemplate*{author}\fi
\ifx\insertdate\@empty\else\usebeamertemplate*{date}\fi
\vfill
\ifx\inserttitlegraphic\@empty\else\inserttitlegraphic\fi
\vspace*{1cm}
\end{minipage}
}
\makeatother
\begin{document}
\maketitle
\section{Section 1: \\ Basics of Stata}
% 1.1 Why learn stata?
\begin{frame}
\frametitle{\textsc{Why learn stata?}}
\begin{center}
\Large \textbf{Excel vs Stata} \\ \normalsize \text{Can I use Excel?}
\end{center}
\end{frame}
\begin{frame}
\frametitle{\textsc{The main reasons to use Stata}}
\begin{itemize}
\item In Excel you make changes directly to the data and save new versions of the data set
\item In Stata you make changes to the instructions on how to get from the raw data to the final analysis and save new versions of the instructions
\item Since Stata is a more statistics oriented software, processing the data to create analytical products can be a lot easier.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{The main reasons to use Stata}}
\begin{itemize}
\onslide<1-> \item Powerful tool with many capabilities:
\begin{itemize}
\item Descriptive statistics
\item Inference statistics
\item Complex data analysis
\end{itemize}
\onslide<2-> \item But it's also good for beginner programmers:
\begin{itemize}
\item User friendly interface
\item Relatively easy programming language that can be learned while you're using the software
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{}}
\begin{center}
\Large \textbf{Stata interface}
\end{center}
\end{frame}
\begin{frame}
\frametitle{\textsc{The Stata interface}}
\begin{figure}[H]
\centering
\includegraphics[width=0.9\linewidth]{stata_interface}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\textsc{The Stata interface - Review window}}
\begin{center}
\Large \textbf{}
\end{center}
\begin{itemize}
\item Provides a history of your actions
\item A convenient way to bring back your previous commands and modify it to do something new
\item Double click on a command you want to use again and it will appear in your command window
\begin{itemize}
\item You can also click in command window and select the commands in the result window by using \textit{PageUp/PageDown} buttons (or \textit{fn+ArrowUp/ fn+ArrowDown} on Mac)
\end{itemize}
\item If a command is \textcolor{red}{red} in the review window, it means it did not finish because an error
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Filtering in variable and review windows}}
\begin{minipage}{0.45\linewidth}
\begin{itemize}
\item Both the variable and the review window will soon be very crowded. You can then search both of them for commands/variables
\item If you do not see the search bar, click the little funnel symbol
\end{itemize}
\end{minipage}
\hfill
\begin{minipage}{0.45\linewidth}
\begin{figure}[H]
\centering
\includegraphics[width=0.75\linewidth]{review_window}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[width=0.75\linewidth]{variable_window}
\end{figure}
\end{minipage}
\end{frame}
\begin{frame}
\frametitle{\textsc{}}
\begin{center}
\Large \textbf{How to open a data set in Stata}
\end{center}
\end{frame}
\begin{frame}
\frametitle{\textsc{Three ways to tell stata what to do}}
\begin{itemize}
\onslide<1-> \item Drop-down menus
\begin{itemize}
\item An easy place to start but quickly becomes inefficient
\end{itemize}
\onslide<2-> \item Command window
\begin{itemize}
\item Faster than menus but require that you are familiar with the command
\end{itemize}
\onslide<3-> \item Do-file
\begin{itemize}
\item The only feasible way to run long instructions
\item Use menus and command window to figure out what you need to write, then copy to a do file
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Open a dataset - menus}}
\begin{figure}[H]
\centering
\fbox{\includegraphics[width=0.9\linewidth]{open_dataset_menu}}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\textsc{Open a dataset - command window}}
\begin{figure}[H]
\centering
\includegraphics[width=0.9\linewidth]{open_dataset_command}
\end{figure}
\begin{itemize}
\item When you use the menus, Stata produces the code for that action (except for Data Browse)
\begin{itemize}
\item Highlight, right-click and copy the code
\item Paste the code in the command window
\item Hit enter
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 1 - opening datasets}}
\begin{enumerate}
\onslide<1-> \item Open Stata and then open the EICV household data set \textbf{cs\_s0\_s5\_household.dta} using the menu: File $\rightarrow$ Open. Navigate to where you saved the material for this lab. Select the data set and click \textit{Open}
\onslide<2-> \item Browse to check that you have data: Data $\rightarrow$ Data Editor $\rightarrow$ Data Editor Browse
\onslide<3-> \item Describe to get additional information on the data: Data $\rightarrow$ Describe data $\rightarrow$ Describe data in memory or in a file.
\begin{itemize}
\item A new window will open
\item Select In memory and press OK
\end{itemize}
\end{enumerate}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 1 - opening datasets}}
\begin{itemize}
\item You can see that one the second command printed information on your screen.
\begin{itemize}
\item The first part is the command used
\item The second part are the results
\end{itemize}
\end{itemize}
\begin{figure}[H]
\centering
\fbox{\includegraphics[width=0.9\linewidth]{task1}}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 1 - opening datasets}}
\begin{itemize}
\item You can perform both tasks by typing the in your command prompt.
This will yield the same results
\item Type \textit{browse} in the command window and press enter
\item Type \textit{describe} and press enter
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{}}
\begin{center}
\Large \textbf{Exploring a data set opened for the first time}
\end{center}
\end{frame}
\begin{frame}
\frametitle{\textsc{Exploring a new dataset}}
\begin{itemize}
\item To successfully clean a data set you must first understand the data set
\item Some terminology:
\begin{itemize}
\item Columns are called variables
\item Rows are called observations
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{The EICV data}}
\begin{itemize}
\item For our exercises we will explore part of EICV 4 data
\item The data is a household survey collected between 2013 and 2014 by NISR
\item It is a cross-section of more than 14 thousand Rwandese households both in rural and urban areas
\item Close to 2 thousand of these households form a panel have been also interviewed in EICV 3
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Types of variables}}
\begin{itemize}
\onslide<1-> \item In Stata, each variable (column) has to be either:
\begin{itemize}
\item string (text): values are red when browsing
\item numeric (number): values are black or blue when browsing
\end{itemize}
\onslide<2-> \item Numbers \textbf{can} be stored as text, but text \textbf{cannot} be stored as number
\begin{itemize}
\item Not possible to do computations on numbers stored as text
\end{itemize}
\onslide<3-> \item Categorical variables should be stored as numeric variables and have labels
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{How the data looks}}
\begin{figure}[H]
\centering
\includegraphics[width=0.9\linewidth]{dataset1}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\textsc{How the data actually is}}
\begin{figure}[H]
\centering
\includegraphics[width=0.9\linewidth]{dataset2}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\textsc{Useful commands}}
\begin{itemize}
\onslide<1-> \item \textit{\underline{br}owse}: see all data in spreadsheet format
\onslide<1-> \item \textit{\underline{d}escribe}: list of all variables in memory
\begin{itemize}
\item Total number of variables \& observations (size of matrix)
\item Variable name, type, format, value label name, variable label
\end{itemize}
\onslide<1-> \item \textit{\underline{su}mmarize}: Basic statistics for numeric variables
\begin{itemize}
\item Obs (Number of observations), Mean, Std. Dev. (Standard deviation), Min (Minimum), Max (Maximum)
\end{itemize}
\onslide<1-> \item \textit{\underline{tab}ulate}: frequencies
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{More commands}}
\begin{itemize}
\onslide<1->\item \textit{codebook}: displays the following for each variable
\begin{itemize}
\item Type (more detail than describe)
\item Number of unique values and number of missing values
\item Range and units
\item Examples of values (strings); tabulations (categorical); or mean, sd and percentiles (continuous)
\item Warnings if embedded blanks (may or may not be ok)
\end{itemize}
\onslide<2->\item \textit{labelbook}: displays the following for each stored value label
\begin{itemize}
\item Label definitions
\item Which variables labels are applied to
\end{itemize}
\onslide<3->\item \textit{\underline{l}ist}: lists all variables and observations
\begin{itemize}
\item Can qualify: \textit{list if price \textless 5000, list in 1/10}
\end{itemize}
\onslide<4->\item \textit{\underline{su}mmarize, \underline{d}etail} : percentiles, variance, skewness, kurtosis
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 2 - exploring a data set}}
\begin{enumerate}
\onslide<1-> \item Open the \textbf{cs\_s0\_s5\_household.dta} again. Use the command prompt this time.
\onslide<2-> \item Explore the dataset
\begin{itemize}
\item browse - see the different colors in the columns
\item describe - check the storage type column
\item summarize - are there any statistics that might not make sense to interpret?
\end{itemize}
\onslide<3->\item Learn more about the variable \textit{s5bq3a}, the household estimated rent amount. What values does it take on? What is minimum, maximum, mean of this variable? How many unique values does it have?
\end{enumerate}
***/
use "$data\cs_s0_s5_household.dta", clear
texdoc stlog, nooutput cmdlog
tabulate s5bq3a
summarize s5bq3a
codebook s5bq3a
texdoc stlog close
/***
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 2 - exploring a data set}}
\onslide<1-> \begin{itemize}
\item Learn more about the variable \textit{ur2012}, to learn about the proportion of urban and rural households in Rwanda
***/
texdoc stlog, nooutput cmdlog
tabulate ur2012
texdoc stlog close
/***
\begin{itemize}
\item Now Create a pie chart: Graphics $\rightarrow$ Pie chart, select \textit{ur2012} as Category variable and press OK
\end{itemize}
\onslide<2-> \item Now, create a pie-chart graph for the variable \textit{s5cq7}, the type drinking water source used. This time, use the command prompt!
\begin{itemize}
\item Use the code printed by the previous graph and replace the name of the variable
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Tips and resources}}
\begin{itemize}
\item Using help - Type \textbf{\textit{help summarize}} to get documentation on the summarize function
\item Using search - Type \textbf{\textit{search regression}} to get general documentation on running regressions in Stata
\item Google - Search what you want to do. There are many resources online (e.g. Statalist)
\end{itemize}
\end{frame}
\section{Section 2: \\ Editing data}
\begin{frame}
\frametitle{\textsc{}}
\begin{center}
\Large \textbf{Editing data in Stata}
\end{center}
\end{frame}
\begin{frame}
\frametitle{\textsc{Deleting variables}}
\begin{itemize}
\item You can delete variables using the commands \textbf{\textit{drop}}
or \textbf{\textit{keep}}
\item Deleting variables is useful to
\begin{itemize}
\item Simplify a very complex data-set for you to work with
\item Reduce computational time when dealing with large data-sets
\item Create temporary subsets of data for analytical purposes, like creating a table or graph
\end{itemize}
\item WARNING: be careful not save the new data on top of the original
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 3 - deleting variables}}
\begin{itemize}
\onslide<1-> \item Open the \textbf{cs\_s0\_s5\_household.dta} data set (use the command prompt)
\end{itemize}
***/
use "$data\cs_s0_s5_household.dta", clear
/***
\begin{itemize}
\onslide<2-> \item Keep the variables we will use in this excerise by typing
\end{itemize}
***/
texdoc stlog, cmdlog
keep hhid province district ur2012 s5cq2 s5cq4 s5cq8 ///
s5cq15 s5cq23 s5bq2 s5cq22 s5cq13 s5cq17
texdoc stlog close
/***
\begin{itemize}
\onslide<3-> \item Now let's say we kept a few variables that we didn't actually needed.To drop them, type
\end{itemize}
***/
texdoc stlog, cmdlog
drop province s5bq2 s5cq17 s5cq15
texdoc stlog close
/***
\end{frame}
\begin{frame}
\frametitle{\textsc{Renaming variables}}
\begin{itemize}
\item You can use the command \textbf{\textit{rename}} to change the names of your variables
\item Renaming is useful as
\begin{itemize}
\item Can make your life easier when programming. Especially when the original variable names don't make much sense
\item It helps you remember what the variable means when a meaningful name is chosen
\item Picking a short variable name reduces time when typing it
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 4 - renaming variables}}
\begin{itemize}
\item Rename all the remaining variables. Type the code below, one line at a time
\end{itemize}
***/
texdoc stlog, cmdlog
rename ur2012 urban_2012
rename s5cq2 m_main_ws
rename s5cq4 m_used_ws
rename s5cq8 m_drink_ws
rename s5cq13 earnings_sell_w
rename s5cq22 d_affected_dis
rename s5cq23 dis_type
texdoc stlog close
/***
\end{frame}
\begin{frame}
\frametitle{\textsc{Generating variables}}
\begin{itemize}
\item You can use the command \textbf{\textit{\underline{gen}erate}} to create new variables
\item Generating variables can be useful to
\begin{itemize}
\item Change the values of a variable to a different measurement unit
\item Create a dummy variable identifying if how many observations have a given characteristic
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 5 - generating variables}}
\begin{itemize}
\item Let us create a variable that converts the number of meters to the main water source to centimeters. Type:
\end{itemize}
***/
texdoc stlog, nooutput cmdlog
generate cm_main_ws = m_main_ws*100
texdoc stlog close
/***
\begin{itemize}
\item Now get descriptives for the new variable using \textit{summarize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 5 - generating variables}}
\onslide<1->\begin{itemize}
\item Let us create a dummy variable (that assumes values 0 or 1) to see if the main water source is the same as the used water source.
\onslide<2-> \item First create a variable that equals zero
\end{itemize}
***/
texdoc stlog, cmdlog
generate d_closest_ws = 0
texdoc stlog close
/***
\begin{itemize}
\onslide<3-> \item Now let's replace that with 1 when it satisfies the condition that the two variables are equal. Type:
\end{itemize}
***/
texdoc stlog, nooutput cmdlog
replace d_closest_ws = 1 if m_main_ws == m_used_ws
texdoc stlog close
/***
\begin{itemize}
\onslide<4-> \item Finally, tabulate the data using the function \textit{tabulate}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Labeling variables and values}}
\begin{itemize}
\item Labeling variables helps understand the variable
\item Value labels indicate what each category of a categorical variable stands for
\item Labeling variables and values is essential for easier understanding in the future by you and others
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 6 - labeling}}
\begin{itemize}
\item Let us create a label for the two variables we created in Task 5
\end{itemize}
***/
texdoc stlog, cmdlog
label variable cm_main_ws "Cm to main water source"
label variable d_closest_ws ///
"Closest water source is used water source"
texdoc stlog close
/***
\begin{itemize}
\item Check the variable window to see the label!
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 6 - labeling}}
\begin{itemize}
\onslide<1-> \item We can also create labels for values with the functions \textit{label define} and \textit{label values}. Type:
\end{itemize}
***/
texdoc stlog, cmdlog
label define yes_no_lb 1 "Yes" 0 "No"
label values d_closest_ws yes_no_lb
texdoc stlog close
/***
\begin{itemize}
\onslide<2-> \item You can see the labels if you tabulate the labeled variable or browse the data
\item This is very useful for binary or categorical variables when visualizing the data
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{}}
\begin{center}
\Large \textbf{How to share your work with your team}
\end{center}
\end{frame}
\begin{frame}
\frametitle{\textsc{You are asked to share your work}}
\begin{itemize}
\item How would you share the work you have done so far?
\item Send only the data set? That would be like Excel and only shares the latest version of the data
\item Nowadays there's a greater demand than to share more than the latest version of the data. We need to show what we did
\item This is where .do files come into the picture
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{What's the fuss about do-files?}}
\begin{itemize}
\item It's through the do-file you communicate your work to other members in your team, both current and future
\item Think of the do-files as instructions on how to get from raw data to final report
\item For a simple task you can enter commands manually. But for more complex tasks you need to write a recipe, or a list of instructions
\item A do-file works similarly to the command window. But instead of running one line of code at the time, a do-file lets you do that run any number of lines of code
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Do-files}}
\begin{itemize}
\item Open up a new do-file. Window $\rightarrow$ Do-file Editor $\rightarrow$ New Do-file Editor
\item Alternatively click the shortcut highlighted below:
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[width=0.9\linewidth]{dofile}
\end{figure}
\end{frame}
\begin{frame}
\begin{itemize}
\item Running the code in your do-file using menus: Tools $\rightarrow$ Execute (Do)
\item Alternatively, select Ctrl+D (Windows) on your keyboard
\item Alternatively, click the shortcut highlighted below
\end{itemize}
\begin{figure}[H]
\centering
\fbox{\includegraphics[width=0.9\linewidth]{run}}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{\textsc{How do you find a file path?}}
\begin{itemize}
\item File path are a integral part of sharing data which we will explain in the next slide
\end{itemize}
\begin{figure}[H]
\centering
\fbox{\includegraphics[width=0.9\linewidth]{file_path}}
\end{figure}
\begin{itemize}
\item When you open any folder, clicking on the \textbf{Home} button on the top right
\item Then clicking on \textbf{Copy} copies the path to the folder you opened
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Macros}}
\begin{itemize}
\item You need to be at least familiar with this topic as this technique is critical especially when projects grow in size
\item Macros (globals, locals, scalar) save some information
(text or number) that you can reference later
\begin{itemize}
\item Example: We want to access files in the folder
multiple times. We can store the folder
location in a global and use it multiple times
\end{itemize}
\item To call a global saved we use the \$ symbol
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 7 - using a do-file}}
\begin{itemize}
\item Open a new do-file. Save it!
\item Type the following in your do file
\end{itemize}
***/
texdoc stlog, cmdlog
clear all
texdoc stlog close
/***
\begin{itemize}
\item Open the folder where you saved the data we shared
\item Copy the file path of this folder
\item Create a \textbf{global} called \textit{data} using the file path you copied
\end{itemize}
***/
texdoc stlog, cmdlog
global data "$dropbox\minagri_stata_training_aug2018\data"
texdoc stlog close
/***
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 7 - using a do-file}}
\begin{itemize}
\item Next, use the review window to copy to your do-file all the actions you already did:
\begin{itemize}
\item Now run the do-file
\item Load the dataset using the global you created
\item Keep only the variables that you need
\item Drop the ones you forgot
\item Rename all the variables
\item Create the cm to the main water source variable
\item Create the dummy if main water source is the same as the used water source
\item Label the variables and values
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 7 - using a do-file}}
This is how your do-file should look now
***/
texdoc stlog, nooutput cmdlog
clear all
use "$data\cs_s0_s5_household.dta", clear
keep hhid province district ur2012 s5cq2 s5cq4 s5cq8 ///
s5cq1 s5cq23 s5bq2 s5cq22 s5cq13 s5cq15 s5cq17
drop province s5bq2 s5cq17 s5cq15
rename ur2012 urban_2012
rename s5cq2 m_main_ws
rename s5cq4 m_used_ws
rename s5cq8 m_drink_ws
rename s5cq13 earnings_sell_w
rename s5cq22 d_affected_dis
rename s5cq23 dis_type
generate cm_main_ws = m_main_ws*100
generate d_closest_ws = 0
replace d_closest_ws = 1 if m_main_ws == m_used_ws
label variable cm_main_ws "Cm to main water source"
label variable d_closest_ws ///
"Closest water source is used water source"
label define yes_no_lb 1 "Yes" 0 "No"
label values d_closest_ws yes_no_lb
texdoc stlog close
save "${data}\cs_s0_s5_household_modified.dta", replace
/***
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 7 - editing a do-file}}
\begin{itemize}
\item Now let's edit the do-file!
\item We just realized that the number of centimeters to the main water source doesn't make much sense. Let's edit it to the number of kilometers. Replace the code:
\end{itemize}
***/
use "${data}\cs_s0_s5_household.dta",clear
rename s5cq2 m_main_ws
texdoc stlog, cmdlog
generate cm_main_ws = m_main_ws*1000
texdoc stlog close
/***
to
***/
use "${data}\cs_s0_s5_household_modified.dta", clear
texdoc stlog, cmdlog
gen km_main_ws = m_main_ws/1000
texdoc stlog close
/***
Also, remember to edit the label before you run the do-file!
\end{frame}
\begin{frame}
\frametitle{\textsc{Comments}}
\begin{itemize}
\item Comments is the green text you have seen in the code examples
\item Comments is text that Stata will ignore when running your code
\item Comments is what makes the difference between instructions that are easy to follow or impossible to understand
\item You can also use comments to omit certain parts of your do-file that you don't want to run anymore, but don't want to erase
\begin{itemize}
\item Maybe you might need it in the future! Just be careful, keeping lots of old code in your do-file might make it messy and hard to understand.
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Different types of comments}}
\begin{center}
\Large\textbf{}
\end{center}
\begin{enumerate}
\onslide<1-> \item \textit{/* comment */}
\begin{itemize}
\item Used for long comments or to explain many lines of code in the following section
\end{itemize}
\onslide<2-> \item \textit{* comment}
\begin{itemize}
\item Used to explain what happens on the following few rows
\end{itemize}
\onslide<3-> \item \textit{ // comment}
\begin{itemize}
\item Used to explain the same line of code
\end{itemize}
\end{enumerate}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 8 - commenting}}
\begin{itemize}
\item Now that you know about comments, add them to your do-file!
\onslide<1-> \item First, add a title and a brief explanation of what your do-file does (e.g. Stata training do-file : Uses EICV4 data to practice Stata, limiting the variables to water usage)
\onslide<2-> \item Now, add a heading to every main section of your do-file (e.g. load data, keep the variables I'll use, create new variables, etc.)
\onslide<3-> \item Finally, we realized that we actually don't need the \textit{km\_main\_ws} variable now, but don't want to erase the code because we might want to use it in the future. Comment out that variable's creation.
\item Run everything!
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 8 - commenting}}
\begin{itemize}
\item Did it work?
\item If you comment out the variable creation and not the labeling, you probably got an error like this:
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[width=0.9\linewidth]{error}
\end{figure}
\begin{itemize}
\item To avoid this, comment out the labeling of the \textit{km\_main\_ws} variable as well.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Saving Stata datasets}}
\begin{itemize}
\item The command for saving a Stata dataset is \textit{\textbf{save}}.
\item \textit{save} saves your data in memory in a file format called dta.
This is a file that can only be read with Stata.
\item The command for saving a dataset in excel and csv is \textit{\textbf{export}}.
\item \textit{\textbf{export}} is the opposite of import, and is very versatile.
It lets you save data in excel, csv, sas and others.
Please refer to the help file on \textit{\textbf{export}}.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\textsc{Saving Stata datasets}}
The help file for the \textbf{\textit{save}} command. \\
The \textbf{\textit{save}} command lets you save your
dataset in the dta format, which is a Stata format which can only be
open with Stata.
\includegraphics[width=\linewidth]{helpfile_save_1}
\includegraphics[width=\linewidth]{helpfile_save_2}
\end{frame}
\begin{frame}
\frametitle{\textsc{Saving Stata datasets}}
The help file for the \textbf{\textit{export excel}} command. \\
\vspace{1mm}
The \textbf{\textit{export excel}} command lets you save your dataset
in excel.
\includegraphics[width=\linewidth]{helpfile_exportexcel_1}
\includegraphics[width=\linewidth]{helpfile_exportexcel_2}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 9 - saving Stata datasets}}
\onslide<1-> Let's save the modified data as a dta file.
\vspace{2mm} Type...
***/
texdoc stlog, cmdlog
save "$data\cs_s0_s5_household_modified.dta", replace
texdoc stlog close
/***
\vspace{2mm}
\onslide<2-> Notice that we use the \textbf{\textit{replace}} option.
This overwrites the existing file.
\vspace{2mm} Type the same command without \textbf{\textit{, replace}},
and see what error you get!
\vspace{2mm}
\onslide<3-> Did you get an error like this?
\begin{center}
\fbox{\includegraphics[width=0.8\linewidth]{error_save_existing}}
\end{center}
\end{frame}
\begin{frame}
\frametitle{\textsc{Task 10 - exporting Stata datasets}}
\onslide<1-> Now, let's save the modified data as a excel.
This is helpful if you are sending the dataset to
someone who does not use or have Stata. \\
Type...
\vspace{2mm}
***/
texdoc stlog, cmdlog
export excel using "$data\cs_s0_s5_household_modified.xls", replace
texdoc stlog close
/***
\vspace{2mm}
\onslide<2-> Open the output file.
Notice that it doesn't have variable names as column names.
This is very inconvenient!
\onslide<3-> Use an optional command, \textbf{\textit{firstrow(variables)}}.
\vspace{2mm}
***/
texdoc stlog, cmdlog
export excel using "$data\cs_s0_s5_household_modified.xls", ///
replace firstrow(variables)
texdoc stlog close