0% found this document useful (0 votes)
522 views2 pages

Change Storage Type in Stata

recast changes the storage type of variables to a different type like byte, int, float, etc. It will refuse to change the type if it would cause data loss or truncation, making it a safe command. The force option overrides these protections and can cause precision loss or missing values but is useful when rounding is acceptable, like changing from double to float. recast works on both numeric and string variables and can promote or demote variable types as needed.

Uploaded by

destiny710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
522 views2 pages

Change Storage Type in Stata

recast changes the storage type of variables to a different type like byte, int, float, etc. It will refuse to change the type if it would cause data loss or truncation, making it a safe command. The force option overrides these protections and can cause precision loss or missing values but is useful when rounding is acceptable, like changing from double to float. recast works on both numeric and string variables and can promote or demote variable types as needed.

Uploaded by

destiny710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Title stata.

com
recast Change storage type of variable
Syntax Description Option Remarks and examples
Also see
Syntax
recast type varlist

, force

where type is byte, int, long, float, double, str1, str2, . . . , str2045, or strL.
Description
recast changes the storage type of the variables identied in varlist to type.
Option
force makes recast unsafe by causing the variables to be given the new storage type even if that
will cause a loss of precision, introduction of missing values, or, for string variables, the truncation
of strings.
force should be used with caution. force is for those instances where you have a variable saved
as a double but would now be satised to have the variable stored as a float, even though that
would lead to a slight rounding of its values.
Remarks and examples stata.com
See [U] 12 Data for a description of storage types. Also see [D] compress and [D] destring for
alternatives to recast.
Example 1
recast refuses to change a variables type if that change is inappropriate for the values actually
stored, so it is always safe to try:
. use http://www.stata-press.com/data/r13/auto
(1978 Automobile Data)
. describe headroom
storage display value
variable name type format label variable label
headroom float %6.1f Headroom (in.)
. recast int headroom
headroom: 37 values would be changed; not changed
Our attempt to change headroom from a float to an int was ignoredif the change had been
made, 37 values would have changed. Here is an example where the type can be changed:
. describe mpg
storage display value
variable name type format label variable label
mpg int %8.0g Mileage (mpg)
1
2 recast Change storage type of variable
. recast byte mpg
. describe mpg
storage display value
variable name type format label variable label
mpg byte %8.0g Mileage (mpg)
recast works with string variables as well as numeric variables, and it provides all the same
protections:
. describe make
storage display value
variable name type format label variable label
make str18 %-18s Make and Model
. recast str16 make
make: 2 values would be changed; not changed
recast can be used both to promote and to demote variables:
. recast str20 make
. describe make
storage display value
variable name type format label variable label
make str20 %-20s Make and Model
Also see
[D] compress Compress data in memory
[D] destring Convert string variables to numeric variables and vice versa
[U] 12.2.2 Numeric storage types
[U] 12.4 Strings

You might also like