While working with dates in Stata, we can convert the remaining string variables (‘date3’ till ‘date9’) to the date type and alter their format as well. gen new_date2 = date(date2,"YMD") format new_date2 %td This variable has dates written with the year first, then the month, and lastly, the day, all separated by dashes. Similarly, we can change the format of the ‘date2’ variable as well. Clicking ‘OK’ after making your selections will generate the following command: format %tdDD/NN/CCYY new_dateīrowsing the variable also reflects the format changes we just made: Note also that in the field at the very bottom, the code for the format has also changed from %td to %tdDD/NN/CCYY. From the second section, we can see that the seventh sample matches this format. But we want our dates to appear in the same format that we saw in the original ‘date1’ string variable, where dates were written in a format that looked like: dd/mm/yyyy. In our case, we have daily data, so “Daily” is an appropriate selection. The first section of this dialogue box offers you various types of data that can be selected. Then click on the three dots that appear at the end of the Format field. In order to do that, the lock sign on the top left corner needs to be clicked to “unlock” the properties so that they can be edited. You can use the Format field in this panel to change the variable’s format as well. Related Article: Filling Gaps in Time series or Panel Data in Stata We can also see from the Properties section in Stata’s right panel that the variable type is float, while its format is %td. You can explore the various formats that data can be displayed in Stata through the command help format.Īfter running the format command, the dates are now displayed in the following manner: If they were to represent the month, we would have written %tm. %tdindicates that dates in the variable represent a single day. In order to do that, the format command is used: format new_date %tdĪfter writing the command format, we write the name of the variable that we would like to change the display format of (‘new_date’ in this case) followed by the format that we would like for the variable to be displayed in. Remember that Stata is able to treat these as a date correctly we just need to change the way they are displayed to understand them. In order for us, the users of Stata, to understand what these numbers mean, we will have to change the (visual) format of the variable. It can also understand correctly what each of these values mean in terms of the date they represent. Here, the type of the variable ‘new_date’ is understood by Stata to be indicating dates. The numeric values generated for each corresponding value of ‘date1’ do not seem to make much sense. So what does the new variable, ‘new_date’ look like? Generally, all the components of date/time and how they are referred to in Stata can be found from Stata’s help section. Here, we want it to have the “DMY” format, which would first have the day, then the month and finally the year. the exact format of the date we would like the new variable to take on. Then, in inverted commas, we specify the order of the date components i.e. In this case, it is the ‘date1’ variable. Firstly, we specify the variable that needs to be converted to date format. There are two parameters of this function. This function can be used when generating a new variable. In order to convert string variables to date format in Stata, we use the date function. If, for example, you wanted to declare your data as a panel data, and had to specify your time variable, using any of the date variables above would give the following error: tsset date1īecause the variable ‘date1’ is a string variable, Stata does not recognize it as a date that can be used to indicate that our data is a panel dataset. Strings are always displayed as red in Stata. This is because ‘closing’ is a numeric variable (float to be specific) but the dates are stored as a string. Notice that these closing prices are displayed in a black colour, while all the dates are displayed in a red colour. There is also one variable called ‘closing’ which stores stock closing prices.
0 Comments
Leave a Reply. |