SAS - Format Data Sets


Advertisements


Sometimes we prefer to show the analyzed data in a format which is different from the format in which it is already present in the data set. For example we want to add the dollar sign and two decimal places to a variable which has price information. Or we may want to show a text variable, all in uppercase. We can use FORMAT to apply the in-built SAS formats and PROC FORMAT is to apply user defined formats. . Also a single format can be applied to multiple variables.

Syntax

The basic syntax for applying in-built SAS formats is:

format variable name format name

Following is the description of the parameters used:

  • variable name is the variable name used in dataset.
  • format name is the data format to be applied on the variable.

Example

Let's consider the below SAS data set containing the employee details of an organization. We wish to show all the names in uppercase. The format statement is used to achieve this.

DATA Employee; 
  INPUT empid name $ salary DEPT $ ; 
  format name $upcase9. ;
DATALINES; 
1 Rick 623.3	IT 		 
2 Dan 515.2 	OPS	
3 Mike 611.5 	IT 	
4 Ryan 729.1    HR 
5 Gary 843.25   FIN 
6 Tusar 578.6   IT 
7 Pranab 632.8  OPS
8 Rasmi 722.5   FIN 
;
RUN;
 PROC PRINT DATA=Employee; 
RUN; 

When the above code is executed, we get the following output.

format1

Using PROC FORMAT

We can also use PROC FORMAT to format data. In the below example we assign new values to the variable DEPT exapnding the name of the department.

DATA Employee; 
  INPUT empid name $ salary DEPT $ ; 

DATALINES; 
1 Rick 623.3 IT 		 
2 Dan 515.2 OPS
3 Mike 611.5 IT 	
4 Ryan 729.1 HR 
5 Gary 843.25 FIN 
6 Tusar 578.6 IT 
7 Pranab 632.8 OPS
8 Rasmi 722.5 FIN 
;
proc format;
value $DEP 'IT' = 'Information Technology'
      'OPS'= 'Operations' ;
RUN;
 PROC PRINT DATA=Employee; 
 format name $upcase9. DEPT $DEP.; 
RUN; 

When the above code is executed, we get the following output.

format2

Advertisements