Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SAS variable


May 26, 2021 SAS


Table of contents


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.

The SAS variable type

SaS has three variable types:

Numeric variables

This is the default variable type. T hese variables are used for mathematical expressions.

Syntactic

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.

Cases

INPUT ID SALARY COMM_PERCENT;

Character variable

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.

Syntactic

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.

Cases

INPUT FNAME $ LNAME $ ADDRESS $;

The date variable

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.

Syntactic

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.

Cases

INPUT DOB DATE11. START_DATE MMDDYY10. ;

Use variables in SAS programs

The above variables are used for SAS programs, as shown in the following example.

Cases

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.

SAS variable

Use variables

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.

SAS variable

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.

SAS variable

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.

SAS variable