SAS Statements & Variables
Share
It is used to tell SAS how to read a variable. It is used to read data in given format. SAS used to store predefined informats . It is used when you read or import data from either an external file or read in sample data which was created using CARDS/DATALINES statement. Every variable in dataset will have an informat . All informat name contain a period.
The character informat $w. tells SAS to read w columns of character data. The w.d format reads numeric values . The w tells SAS how many columns to read . The optional d tells SAS that there is an implied decimal point in the value. For example , if you have the number 123 and you read it as 3.0 informat , SAS stores it as 123.0 . If you read the same number with 3.1 informat , SAS stores it as 12.3 .
There are three main classes of informats:
Type Informat Name Description
Character $w. Reads character data of length w.
Numeric w.d Reads numeric data of length w with d decimal points
Date MMDDYYw. Reads date data in the form of 10-01-95
We create a new dataset having name and salary variables. We used informat on salary variable to read comma formatted data.
data new ;
input name $ salary comma6.;
datalines;
Sam 4,545
Ram 8,546
;
run;
Output:
We create a new dataset to read dollar formatted salary variable.
data new;
input id salary;
informat salary dollar8.;
datalines;
11 $1,245
12 $4,586
;
run;
Output:
We create a new dataset to read comma formatted salary variable by using dollar8 informat .
data new;
input id salary;
informat salary dollar8.;
datalines;
11 1,245
12 4,586
;
run;
Output:
Format Statement
It is used to tell SAS how to display the values in the variable. SAS used to store predefined formats .
The format for a variable does not have to be same as the informat for the variable. The format statement can be used in either a data step or a proc step.
- Assigning a format to a variable in a data step will permanently store the variable’s format in the dataset
- Assigning a format to a variable in a proc step will temporarily store the variable’s format assignment for the proc step.
Formats can be grouped into three classes:
Type Informat Name Description
Character $w. Displays character data of length w.
Numeric w.d Displays numeric data of length w with d decimal points
Date MMDDYYw. Displays date data in the form of 10-01-95
We apply format on x as 6.2 which means maximum length of 6 digits and included 2 decimal points.
Data new;
input x 6.;
format x 6.2;
datalines;
1245
123.4512
4578.12
;
run;
Output:
We create a new dataset with format on salary as dollat6. It represent salary in dollar($) form. The total length of salary is 6 bytes.
data new;
input id salary;
format salary dollar6.;
datalines;
102 4521
103 4875
;
run;
Output:
We used comma6. to represent salary in comma form. The total length of salary is 6 bytes.
data new ;
input name $ salary comma6.;
format salary comma6.;
datalines;
Sam 4,545
Ram 8,546
;
run;
Output:
When we apply comma4. format on salary then it does not display dollar($) and comma(,).
data new ;
input name $ salary comma6.;
format salary comma4.;
datalines;
Sam 4,545
Ram 8,546
;
run;
Output:
We used comma6. as informat and dollar8. as format on salary.
data new;
input name$ salary comma6.;
format salary dollar8.;
datalines;
Dhruv 4,578
Kannu 45,457
Fred 7,548
;
run;
Output:
We create a new dataset “workers1” as :
data workers1;
length city$10.;
/* Specifying the length for variable City */
input city$ ID$ Gender$ Salbegin;
/* Input Statement to declare variables*/
format Salbegin dollar8.;
datalines;
Gurgaon G101 M 27000
Kolkata K102 F 12000
Hyderabad H103 M 30000
;
run;
Output:
Date Variables
The date values in SAS are stored as the number of days since January 1, 1960 . The stored date values can be negative , if the date is before January 1 , 1960 or positive , if the date is after January 1 , 1960 . For example , the date June 30 , 1997 will be stored in SAS as 13695 because June 30, 1997 was 13,695 days after January 1 , 1960.
We have different types of informats and formats available :
ddmmyy format
It stores date in day/month/year format . If we assign ddmmyy10. as informat , it will read date 12/10/1986 and store it in numeric digit.
We create a new dataset to read ddmmyy10. as date format .
data new;
input date ddmmyy10.;
datalines;
12/10/1985
04/11/1986
;
run;
It stores the date as numeric digits . In the output, date variable shows 9416 days after January 1 , 1960 as October 12 , 1985 . Similarly , November 4, 1986 shows as 9804 days after January 1 , 1960.
Output:
We create a new dataset to store date in ddmmyy10. format.
data new;
input date ddmmyy10.;
format date ddmmyy10.;
datalines;
12/10/1985
04/11/1986
;
run;
Output:
date format
We are using date. format to display date in daymonthyear format . If we have date as 12/10/1985 , it store as 12OCT1985 . If we used format as date8. then it will store as 12OCT85 .
We create a new dataset to read date as ddmmyy. format , and store it as date. format.
data new;
input date ddmmyy10.;
format date date10.;
datalines;
12/10/1985
04/11/1986
;
run;
Output:
We used date8. as date format to store date in this format .
data new;
input date ddmmyy10.;
format date date8.;
datalines;
12/10/1985
04/11/1986
;
run;
Output:
We create new dataset “diet” to store date and time formats . We used time. format to read time values
DATA diet;
input subj 1-4 l_name $ 18-23 weight 30-32
+1 wt_date mmddyy8. @43 b_date mmddyy8.
@52 wt_time time8.;
wtm_fmt1 = wt_time;
wtm_fmt2 = wt_time;
wtm_fmt3 = wt_time;
format wtm_fmt1 hhmm.
wtm_fmt2 hour5.2
wtm_fmt3 time8.;
DATALINES;
1024 Alice Smith 1 65 125 12/1/05 01/01/60 00:01:00
1167 Maryann White 1 68 140 12/01/05 01/01/59 00:15:00
1168 Thomas Jones 2 190 12/2/05 06/15/60 12:00:00
1201 Benedictine Arnold 2 68 190 11/30/05 12/31/60 00:00:00
1302 Felicia Ho 1 63 115 1/1/06 06/15/58 23:59:59
;
RUN;
The wt_time variable store in seconds . It converts time values into seconds. As , you can see wedefine format of wtm_fm1 as hhmm. which stores values in hour :minute format. we used hour5.2 formate to display wtm-fmt2 in hour format . We used time8. format to represent time in hour:minute:second format.
Output :