The SAS Programming involves first creating/reading the data sets into the memory and then doing the analysis on this data. We need to understand the flow in which a program is written to achieve this.
The below diagram shows the steps to be written in the given sequence to create a SAS Program.
Every SAS program must have all these steps to complete reading the input data, analysing the data and giving the output of the analysis. Also the RUN statement at the end of each step is required to complete the execution of that step.
This step involves loading the required data set into SAS memory and identifying the variables (also called columns) of the data set. It also captures the records (also called observations or subjects). The syntax for DATA statement is as below.
DATA data_set_name; #Name the data set. INPUT var1,var2,var3; #Define the variables in this data set. NEW_VAR; #Create new variables. LABEL; #Assign labels to variables. DATALINES; #Enter the data. RUN;
The below example shows a simple case of naming the data set, defining the variables, creating new variables and entering the data. Here the string variables have a $ at the end and numeric values are without it.
DATA TEMP; INPUT ID $ NAME $ SALARY DEPARTMENT $; comm = SALARY*0.25; LABEL ID = 'Employee ID' comm = 'COMMISION'; DATALINES; 1 Rick 623.3 IT 2 Dan 515.2 Operations 3 Michelle 611 IT 4 Ryan 729 HR 5 Gary 843.25 Finance 6 Nina 578 IT 7 Simon 632.8 Operations 8 Guru 722.5 Finance ; RUN;
This step involves invoking a SAS built-in procedure to analyse the data.
PROC procedure_name options; #The name of the proc. RUN;
The below example shows using the MEANS procedure to print the mean values of the numeric variables in the data set.
PROC MEANS; RUN;
The data from the data sets can be displayed with conditional output statements.
PROC PRINT DATA = data_set; OPTIONS; RUN;
The below example shows using the where clause in the output to produce only few records from the data set.
PROC PRINT DATA = TEMP; WHERE SALARY > 700; RUN;
Below is the complete code for each of the above steps.