
Stata has commands for dropping duplicates, but it is also important to understand why there a duplicates, because there might be something else wrong with your data.


#Summarize stata software
Stata won't let you merge another dataset if _merge is already there.Ī common problem with merging occurs when there are duplicate observations, which prevent the software from matching. Stata provides you with a handy variable called _merge that identifies if observations matched (3), were only in the master file (1) or only in the using file (2). What do imagine you would use if you were merging two datasets, each with only one observation for each value of the key variable?Īfter you have merged, you should check to see what didn't merge. Your master dataset has many observations for each state, but the using dataset has only one observations for each state. Merge m:1 statefips using statepolitical.dta in_class_data.dta is called the "master" file, while statepolitical.dta will be the "using" file. We want to merge the statepolitical dataset to in_class_data, so we want in_class_data to be open while we are merging. Now let's go back to the previous dataset. For countries, there are various codes developed by the World Bank, IMF, etc., and all are preferable to using the names of the country the Stata command kountry works similarly to statastates.
#Summarize stata code
states, look for a FIPS code or postal code rather than the name of the state the Stata command statastates can be used to add them if they are not provided. It's better to use a numeric or alphanumeric code rather than a name to perform a merge. In both datasets, we have variables that describe states. The first thing to do is identify a variable that appears in both datasets, and that is stored in exactly the same way, which will allow the software to match up information in both datasets. In this example, we have two datasets, in_class_data and statepolitical, and we want to use variables from both in one analysis. Save in_class_data, replace Combining datasets It's a good idea to keep a copy of your original, pristine data, so let's save under a different filename. None of these changes will persist until we save the data.

You can also get rid of variables that don't meet certain conditions. If you don't need the variable anymore, you can drop it. Otherwise, it will create missing values. You should also use the ignore() option to skip over any characters that aren't numbers. Replace married = 0 if marst != 3 Using destringĪs in encode, you can (and should) create a new variable using the gen() option. We can easily create a dummy variable for married/not married. With the encode command, add the gen() option to put the newly created numeric values in a new variable.Ĭodebook is very handy for seeing what the actual values of the new variable are. Since the file is in Stata format (.dta) and it is in your working directory (right?), we can open with simply:įirst, use describe to reassure yourself that it is a string. Our sample dataset has an example of each. (This often happens when the values contain dollars signs, commas, etc.) In this case, you should use destring.

They take up more memory and are not useful for your regressions, etc. When you want to stop the log, end by closing the log file.Ī major pitfall: string variables. Log using stataworkshop.txt, text replace The log file can be turned on and off, but it is a good idea to help you keep track of what you've done. Log filesĪ log file records the output of the commands as you run your code. It could be /* like this */ for the middle of a line. Stata will ignore the text of the comments when you run your code, but they make your. You can create a new do-file by clicking on the New Do-File Editor button, or typingĪ properly documented do-file will contain comments that communicate your intentions at each step of code. With a correctly written do-file, anyone can reproduce your analysis. Do-files are scripts for automating Stata commands.
