Sometimes you have data in SAS/IML vectors that you need to write to a SAS data set. By default, no formats are associated with the variables that you create from SAS/IML vectors. However, some variables (notably dates, times, and datetimes) should have formats associated with the data values. You can associate a format with a vector by using the MATTRIB statement. Then when you create a SAS data set, the data set variable automatically gets the same format.
As an example, the following SAS/IML program defines data for the height of a hypothetical child. A vector of dates records when the heights were measured, and a vector of percentiles indicates how the child's height compared to the heights of his peers. You can use the FORMAT= option on the print statement to display the data in a readable form:
proc iml; /* growth chart (cm) for hypothetical child */ Date={'15MAR10'd, '22MAR11'd, '12MAR12'd, '18MAR13'd, '20MAR14'd }; Height = {133.9, 139.6, 144.2, 157.0, 168.1}; Pctl = { 0.25, 0.30, 0.50, 0.65, 0.75}; print Date[format=DATE9.] Height[format=3.0] Pctl[format=PERCENT7.]; |
The DATE format is essential for understanding the data, so it would be nice to include that format (and the format for the other variables) when the data are written to a SAS data set. The MATTRIB statement makes this process easy:
mattrib Date format=DATE9. Height format=3.0 Pctl format=PERCENT7.; create Growth var {"Date" "Height" "Pctl"}; append; close Growth; quit; ods select Variables; proc contents data=Growth; run; |
As a bonus, the PRINT statement in SAS/IML uses the formats that are assigned by the MATTRIB statement. Consequently, if you assign formats by using the MATTRIB statement, you do not need to use the FORMAT= option when you print the vectors in SAS/IML.