buildhost.blogg.se

Tostring stata
Tostring stata







Note that in this case we have to tell STATA whether “08”is the year 1908 or 2008. Here the variable date_string2 is similar to the one before, but the year component has only two digits rather than four. The second example is slightly more complex. So we can extract the date1 variable in only one line of code. The “MDY” option tells STATA that the date_string1 variable has a month, day, and year – appearing in that specific way. Using the date command you can fix this variable just by telling STATA what this string contains. While here as well you could parse the string into pieces and build a date variable as we did before, you can also use a shortcut to get your proper date variable in one line of code.Ĭonsider variable called date_string1. Maybe the most common situation that you will come across is that your date variable gets imported as string (see data snapshot below). *combine the three components to build a daily date variableįormat %td date_proper FIXING DATE VARIABLES: Example 2

tostring stata

*destring the three date components to numeric variables *extract parts of the date_string variable

tostring stata

Finally, the three components are destring-ed (converted to numeric) and combined into monthly date using the mdy (month,day,year) command. Next, that string is parsed into three components using the substr (stands for sub-string) command. The code first converts the variable to a string.

tostring stata

While this can be fixed in many ways, I suggest the following approach which I hope even STATA beginners will be able to digest easily. In the example below, we start with a variable that is simply a number. Below I give a few examples that should help with that.

#TOSTRING STATA SOFTWARE#

What is often the case is that after importing data from other software such as Microsoft Excel you don’t have such proper STATA date variable. What I mean by proper is a date variable formatted in a way that STATA understands that it is a date variable. All commands above assumed you already have a proper daily date variable.







Tostring stata