I looked up this code in the Command Syntax Reference Manual (available through the Help menu) and there it was—a very useful command which can be applied in many applications.
In brief, it does exactly what the name implies. It changes the Variable Type (string or numeric) or Format of variables, including the Width of string variables. As I was reading the explanation, it appears to be a new and improved Format statement, a so-called Format on steroids!
Format statements are often used to change the width and decimals of numeric variables or the format of a date variable. The Alter Type command changes the Variable Type of any variable in one short command—no need to write elaborate or unnecessary code…just one easy statement.
As an example, the dataset below comprises 3 variables and 2 lines of data.
DATA LIST FREE
/Numvar (F2) StringVar (A5) Datevar (Adate10).
1 1234 10/28/2007
4 5678 10/28/2007
- To change a numeric variable to a string (alphanumeric) variable, the command is:
Alter Type Numvar (A2).
- To change a string (alphanumeric) variable to a numeric variable, the command is:
Alter Type Stringvar (F6.0).
- To change a date variable to a string variable, the command is:
One note of caution: The Alter Type command does not allow you to create a new version of the variable. So you may want to save your data first or create a copy of the variable.
So, the next time you need to perform a calculations or merge data and the variable is not in the right Format or Type, use the Alter Type command. After all, it’s free, you are not crazy, and it’s cool!