Transcription of Stata: Software for Statistics and Data Science | Stata
1 Set variables output formatSyntaxMenuDescriptionOptionRemarks and examplesReferencesAlso seeSyntaxSet formatsformatvarlist%fmtformat %fmt varlistSet style of decimal pointset dp{comma|period}[, permanently]Display long formatsformat[varlist]where%fmtcan be a numerical, date, business calendar, or string format Set variables output formatNumerical%fmtDescriptionExamplerig ht-justified%#.#ggeneral% #.#ffixed% #.#eexponential% , hilo%16H%16 Lbinary, lohi%16L%8 Hbinary, hilo%8H%8 Lbinary, lohi%8 Lright-justified with commas%#.#gcgeneral% #.#fcfixed% with leading zeros%0#.#ffixed% #.#ggeneral% #.#ffixed% #.#eexponential% with commas%-#.#gcgeneral% #.#fcfixed% may substitute comma (,) for period (.) in anyof the above formats to make comma the decimal point. In%9,2fc, is ,03. Or you canset dp are many variations allowed.
2 See [D]datetime display Set variables output format 3business calendar%fmtDescriptionExample%tbcalname a business%tbsimple[:datetime-specifiers]c alendar defined [D]datetime business #sstring%15sleft-justified%-#sstring%-20 scentered%~#sstring%~12sThe centered format is for use >Variables ManagerDescriptionformatvarlist%fmtandfo rmat %fmt varlistare the same commands. They set the display formatassociated with the variables specified. The default formats are a function of the type of the variable:byte % % % % % #%#sstrL %9sset dpsets the symbol that Stata uses to represent the decimal point. The default isperiod,meaning that one and a half is displayed as [varlist]displays the current formats associated with the itself listsall variables that have formats too long to be listed in their entirety the formats for the specified variables regardless of their *lists the formats forall the that, in addition to making the change right now, thedpsetting be rememberedand become the default setting when you invoke format Set variables output formatRemarks and are presented under the following headings:Setting formatsSetting European formatsDetails of formatsThe %f formatThe %fc formatThe %g formatThe %gc formatThe %e formatThe %21x formatThe %16H and %16L formatsThe %8H and %8L formatsThe %t formatThe %s formatOther effects of formatsDisplaying current formatsSetting formatsSee[U] Formats.
3 Controlling how data are displayedfor an explanation of%fmt. To review: Stata s three numeric formats are denoted by a leading percent sign,%, followed by the (orw,dfor European format), wherewanddstand for two integers. The first integer,w, specifiesthe width of the format. The second integer,d, specifies the number of digits that are to follow thedecimal point;dmust be less thanw. Finally, a character denoting the format type (e,f, org) isappended. For example,% thefformat that is nine characters wide and has two digitsfollowing the decimal point. Forfandg, acmay also be suffixed to indicate comma formats. Other numeric formats known collectively as the%tformats are used to display dates and times; see[D]datetime display formats. String formats are denoted by%ws, wherewindicates the width ofthe 1We have census data by region and state on median age and population in use (1980 Census data by state).
4 DescribeContains data from : 50 1980 Census data by statevars: 4 9 Apr 2013 08:05size: 1,200storage display valuevariable name type format label variable labelstate str14 %14s Stateregion int % cenreg Census regionpop long % Populationmedage float % Median ageSorted by:format Set variables output format 5. list in 1/8state region pop South 3893888 West 401851 West 2718215 South 2286435 West 23667902 West 2889964 NE 3107576 South 594338 has a display format of%14s. To left-align the state data, we type.
5 Format state %-14s. list in 1/8state region pop South 3893888 West 401851 West 2718215 South 2286435 West 23667902 West 2889964 NE 3107576 South 594338 it seems likeregionis a string variable, it is really a numeric variable with an attachedvalue label. You do the same thing to left-align a numeric variable as you do a string variable: inserta negative format region % list in 1/8state region pop South 3893888 West 401851 West 2718215 South 2286435 West 23667902 West 2889964 NE 3107576 South 594338 format Set variables output formatThepopvariable would probably be easier to read if we inserted commas by appending a c.
6 Format pop % list in 1/8state region pop South 3,893,888 West 401,851 West 2,718,215 South 2,286,435 West 23667902 West 2,889,964 NE 3,107,576 South 594,338 at the value ofpopfor observation 5. There are no commas. This number was too large forStata to insert commas and still respect the current width of 11. Let s try again:. format pop % list in 1/8state region pop South 3,893,888 West 401,851 West 2,718,215 South 2,286,435 West 23,667,902 West 2,889,964 NE 3,107,576 South 594,338 ,medagewould look better if the decimal points were vertically format medage % list in 1/8state region pop South 3,893,888 West 401,851 West 2,718,215 South 2,286,435 West 23,667,902 West 2,889,964 NE 3,107,576 South 594,338 formats are permanently attached to variables by theformatcommand.
7 If wesavethedata, the next time weuseit,statewill still be formatted as%-14s,regionwill still be formattedas% , Set variables output format 7 Example 2 Suppose that we have an employee identification variable,empid, and that we want to retain theleading zeros when we list our a leading-zero option that allows use describe empidstorage display valuevariable name type format label variable labelempid float % list empid in 83 format empid % list empid in 83 noteThe syntax of theformatcommand allows avarlistand not just one variable name. Thus youcan attach the% to the variablesmyvar,thisvar, andthatvarby typing. format myvar thisvar thatvar % 3We have employee data that storedas afloat, but we were careful to storeloginandlogoutasdoubles. We need to attach a dateformat to these three use format hiredate login logoutvariable name display formathiredate % % % format Set variables output format.
8 Format login logout %tcDDmonCCYY_HH: list login logout in 1/5login 08:16 08nov2006 05:32 08:07 08nov2006 05:57 08:10 08nov2006 06:17 08:30 08nov2006 05:42 08:29 08nov2006 05:29 format hiredate %td. list hiredate in 1 remember that the project manager requested that hire dates be presented in the same form asthey were format hiredate %tdDD/NN/CCYY. list hiredate in 1 European formatsDo you prefer that one and one half be written as 1,5 and that one thousand one and a half bewritten as ,5? Stata will present numbers in that format if, when you set the format, you specify , rather than . as follows:. use (1980 Census data by state). format pop %12,0gc. format medage %9,2fformat Set variables output format 9. list in 1/8state region pop South 29, West 26, West 29, South 30, West 29, West 28, NE 32, South 29,80 You can also leave the formats just as they were and instead typeset dp comma.
9 That tells Stata tointerpret all formats as if you had typed the comma instead of the period:. format pop % (put the formats back as they were). format medage % set dp comma(tell Stata to use European format). list in 1/8(same output appears as above)set dp commaaffects all Stata output, so if you run a regression, display summary Statistics , or makea table, commas will be used instead of periods in the output:. tabulate region [fw=pop]CensusregionFreq. Percent 21,75 21,75N 26,06 47, 33,08 80, 19,11 100, 100,00 You can return to using periods by typing. set dp periodSetting a variable s display format to European affects how the variable s values are displayed bylistand in a few other places. Settingdptocommaaffects every bit of ,set dp commaaffects only how Stata displays output, not how it gets input.
10 When you needto type one and a half, you must of noteset dp commamakes drastic changes inside Stata , and we mention this because some older, user-written programs may not be able to deal with those changes. If you are using an older, user-writtenprogram, you mightset dp commaonly to find that the program does not work and instead presentssome sort of syntax , using any program, you get an unanticipated error, try settingdpback withset dp comma, you might still see some output with the decimal symbol shown as aperiod rather than a comma. There are two places in Stata where Stata ignoresset dp commabecausethe features are generally used to produce what will be treated as input, andset dp commadoes notaffect how Stata inputs numbers. First,10 format Set variables output formatlocal x = sqrt(2)stores the string inxand not 1,414213562373095 , so if some programwere to display x as a string in the output, the period would be displayed.