There are times we need to do some repetitive tasks in the process of data preparation, analysis or presentation, for instance, computing a set of variables in a same manner, rename or create a series of variables, or repetitively recode values of a number of variables. In this post, I show a few of simple example “loops” using Stata commands -foreach-, -local- and -forvalues- to handle some common simple repetitive tasks.
Multilevel regression for interval-measured outcomes. Incomes are sometimes recorded in groupings, as are people's weights, insect counts, grade-point averages, and hundreds of other measures. Often we have repeated measurements for individuals, or schools, or orchards, etc. Stata Tips #8 - Spatial Analysis in Stata 15. One of the new features in Stata 15 is spatial autoregressive models. These account for correlation between points or areas in space in the same way that time series models include correlation over time. To work with these, we typically have some data where each observation belongs to a point.
-foreach-: loop over items
Consider this sample dataset of monthly average temperature for three years.
Now the mean temperatures of each month are in Centigrade, if we want to convert them to Fahrenheit, we could do the computation for the 12 variables.
However this takes a lot of typing. Alternatively, we can use the -foreach- command to achieve the same goal. In the following codes, we tell Stata to do the same thing (the computation: c*9/5+32) for each of the variable in the varlist – mtemp1 to mtemp12.
Note that braces must be specified with -foreach-. The open brace has to be on the same line as the foreach, and the close brace must be on a line by itself. It’s crucial to close loops properly, especially if you have one or more loops nested in another loop.
-local-: define macro
This was a rather simple repetitive task which can be handled solely by the foreach command. Here we introduce another command -local-, which is utilized a lot with commands like foreach to deal with repetitive tasks that are more complex. The -local- command is a way of defining macro in Stata. A Stata macro can contain multiple elements; it has a name and contents. Consider the following two examples:
Define a local macro called mcode and another called month, alter the contents of mcode in the foreach loop, then display them in a form of “mcode: month”.
Note when you call a defined macro, it has to be wrapped in “`” (left tick) and “‘” (apostrophe) symbols.
Rename multiple variables
Take the temperature dataset we created as an example. Let’s say we want to rename variables mtemp1-mtemp12 as mtempjan-mtenpdec. We can do so by just tweaking a bit of the codes in the previous example.
Define local macro mcode and month, then rename the 12 vars in the foreach loop.
We can obtain the same results in a slightly different way. This time we use another 12 variables fmtemp1-fmtemp12 as examples. Again, we will rename them as fmtempjan-fmtempdec.
Define local macro month, then define local macro monthII in the foreach loop with specifying the string function word to reference the contents of the local macro month.
I usually run -display- to see how the macro looks like before actually applying the defined macro on tasks like changing variable names, just to make sure I don’t accidentally change them to some undesired results or even cause errors; however the display line is not necessary in this case.
Here we rename them back to fmtemp1-fmtemp12.
-forvalues-: loop over consecutive values
The -forvalues- command is another command that gets to be used a lot in handling repetitive works. Consider the same temperature dataset we created, suppose we would like to generate twelve dummy variables (warm1-warm12) to reflect if each of the monthly average temperature is higher than the one in the previous year. For example, I will code warm1 for the year of 2014 as 1 if the value of fmtemp1 for 2014 is higher than the value for 2013. I will code all the warm variables as 99 for the year of 2013, since they don’t have references to compare in this case.
We can do this by running the following codes, then repeat them for twelve times to create the twelve variables warm1-warm12.
Stata 15 With Crack
However this takes a lot of typing and may even create unwanted mistakes in the process of typing or copy-paste them over and over.
Stata 15 Without
Instead, we can use -forvalues- to do so:
Reference
Baum, C. (2005). A little bit of Stata programming goes a long way… Working Papers in Economics, 69.
Stata 15 With Key
View the entire collection of UVA Library StatLab articles.
Yun Tai
CLIR Postdoctoral Fellow
University of Virginia Library
Stata 15 Download
- Jul 2014
- 55
Stata 15 compatibility problem
Dear forum,
Recently our university HPCs have upgraded to Stata15 while the general PCs have remained with Stata14. I have created few graphs in HPC (as my dataset is very large) in the batch mode connected through MobaXterm terminals. When I tried to open these gph files in Stata14, it gives an error message, 'file .......is a new format that this version of Stata does not know how to read'. I would imagine I should have saved the files in the old version, but unfortunately the saveold command is not working here.
Any suggestion is highly appreciated.
Ujjwal Kumar Das
PhD Student, Economics
University of Leeds
United Kingdom, LS2 9JT- Mar 2014
- 25410
saveold is about saving datasets and is nothing whatsoever to do with saving graphs.
I don't think there is a Stata solution to this. Stata 14 doesn't understand Stata 15 graph formats.
There is plenty of scope to save the graphs to some other graphic format so that you can look at the graphs otherwise.Comment
- Jul 2014
- 55
Dear Nick, Thanks for your answer. I have tried other options but the problem persists. When I used
. graph export '.png'
translator Graph2png not found
r(111);
This seems to be a problem of Linux platform.
Then I used
. graph save Graph '.png'
(file .png saved)
. graph save Graph '.tif'
(file .tif saved)
This time the created files are simply blank. Any suggestions please.Comment
- Mar 2014
- 25410
I can't tell you anything about your network or set-up but
1. It's documented that 'png and tif are available for all versions of Stata except Stata(console)'
2. I would certainly recommend giving a full informative name such as 'histogram.png' and never just a bare extension.Comment
- Dec 2014
- 8177
Like Nick, I'm largely in the dark on this, but
1) I think it is graph export you need; help graph save does not document using it to change the format of the files.
2) Please see the Technical Note in the documentation for the graph export command found in the Stata Graphics Reference Manual PDF for Stata 15. Try adding the name() option to the graph export command, and try using the EPS format since there are circumstances under which png and tiff are not usable.
3) If you are creating your graphs using the graph commands directly, is it possible to create a reduced copy of your dataset containing just what is needed to produce the graph, and then move the reduced dataset to a PC running Stata 14 and create your graph there?
4) You could bring this issue to the attention of the university group(s) responsible for supporting the HPC and the PCs. In general, it is possible to have two versions of Stata installed simultaneously on the same system, so perhaps the HPC should continue to make Stata 14 available for compatibility with the PCs until such time as everything ha Stata 15 available. You might raise this issue through your department - talk to your advisor.Comment
- Mar 2014
- 25410
I didn't notice (thanks William!) you were using graph save for a task for which the documented command is graph export
In your defence it should be noted that it's not 100% clear in the documentation that the most this command does is save in Stata's own .gph format; specifying another extension changes only the name.
For example -- trust me that I had a graph up when I did this -- I carried out this experiment
It is the same file, as can verified by typing it or looking in a text editor.Comment
- Jul 2014
- 55
Thanks Nick and William. I have checked further documentation and also spoken to the HPC technical team at my Uni. This is partly a problem of Stata UNIX platform which does not support graph export png or tif but support eps format. Also I need Stata 15 on my pc (we currently have Stata 14.1) to open gph files created by Stata 15 on HPC. However, I have solved my problem by creating the graph at HPC using Stata 14.Comment