May 26, 2021 SAS
In general, a variable in SAS represents the column name of the data table it is analyzing. B ut it can also be used for other purposes, such as using it as a counter in a programming loop. I n this chapter, we'll see the use of SAS variables as column names for SAS datasets.
SaS has three variable types:
This is the default variable type. T hese variables are used for mathematical expressions.
INPUT VAR1 VAR2 VAR3; #Define numeric variables in the data set.
In the syntax above, the INPUT statement displays the declaration of the numeric variable.
INPUT ID SALARY COMM_PERCENT;
Character variables are used for values that are not used in mathematical expressions. T hey are treated as text or strings. T he variable becomes a character variable by adding a $sing with a space at the end of the variable name.
INPUT VAR1 $ VAR2 $ VAR3 $; #Define character variables in the data set.
In the syntax above, the INPUT statement displays the declaration of the character variable.
INPUT FNAME $ LNAME $ ADDRESS $;
These variables are processed only as dates, and they need to be in a valid date format. T he variable changes to a date variable by adding a date format with a space at the end of the variable name.
INPUT VAR1 DATE11. VAR2 MMDDYY10. ; #Define date variables in the data set.
In the syntax above, the INPUT statement displays the declaration of the date variable.
INPUT DOB DATE11. START_DATE MMDDYY10. ;
The above variables are used for SAS programs, as shown in the following example.
The following code shows how to declare and use three types of variables in an SAS program
DATA TEMP; INPUT ID NAME $ SALARY DEPT $ DOJ DATE9. ; FORMAT DOJ DATE9. ; DATALINES; 1 Rick 623.3 IT 02APR2001 2 Dan 515.2 OPS 11JUL2012 3 Michelle 611 IT 21OCT2000 4 Ryan 729 HR 30JUL2012 5 Gary 843.25 FIN 06AUG2000 6 Tusar 578 IT 01MAR2009 7 Pranab 632.8 OPS 16AUG1998 8 Rasmi 722.5 FIN 13SEP2014 ; PROC PRINT DATA=TEMP; RUN;
In the example above, all character variables are declared with a $symbol, and date variables are declared with a date format. T he output of the above program is as follows.
These variables are useful when analyzing data. T hey are used in expressions that apply statistical analysis. L et's look at an example of analyzing a built-in dataset called CARS, which is located under Libraries - My Libraries - SASHELP. D ouble-click it to explore variables and their data types.
Next, we can use the task options in SAS studio to generate a summary of some of these variables. G o to task - statistics - summary statistics and double-click it to open the window as shown below. S elect the dataset SASHELP. C ARS and select the three variables under the analysis variables - MPG_CITY, MPG_Highway and Weight. H old down the Ctrl key while clicking select a variable. C lick Run.
After the steps above, click the Results tab. I t shows a statistical summary of the three variables selected. T he last column represents the number of observations (records) used in the analysis.