The document discusses SAS formats and provides examples of their use. It covers three main categories of SAS formats: informats, formats, and user-defined formats including picture and lookup table formats. Examples are provided to demonstrate converting between numeric and character data, applying date and time formats, and using PROC FORMAT to create user-defined formats from lookup tables to summarize fees in a business example. References to online documentation and sample code are also included.
2. • Three main categories of SAS Formats
• Informats & Formats
• Conversion
• User Defined Formats
• Picture Formats
• Lookup tables
• Business example using Formats from lookup tables
• References
Page 2
7. PROC FORMAT;
Value $SEX 'M'='Male'
'F'='Female'
Other='Unknown';
Quit;
CodeN=Male
CodeT=Male
Score=C
Data _null_;
CodeN=Put(1, Sex.);
CodeT=Put('M', $Sex.);
Score=Put('M', $Score.);
Put CodeN= CodeT= Score=;
Run;
SAS PROC FORMAT SAS Data Step
Output
$FMTNAME Character
FMTNAME Numeric
PROC FORMAT;
Value $SEX 'M'='Male'
'F'='Female'
Other='Unknown';
Value SEX 1='Male'
2='Female'
Other='Unknown';
Quit;
PROC FORMAT;
Value $SEX 'M'='Male'
'F'='Female'
Other='Unknown';
Value SEX 1='Male'
2='Female'
Other='Unknown';
Value $Score 'a'-<'e','A'-<'E'='A'
'e'-<'m','E'-<'M'='B'
'm'-'z~','M'-'Z~'='C';
Quit;
Range
Page 7
8. PROC FORMAT CNTLIN=Lookup Table
Start
End <optional>
HLO <optional>
Type <optional>
PROC FORMAT CNTLOUT=Lookup Table
Label
FmtName
Activity_ID Activity Description Fee
1 Letter $8.50
2 Phone - Call $9.00
3 Phone - Receive $5.00
4 Account Open $5.00
5 Account Close $3.00
Data Lookup
Start Label FMTNAME
1 $8.50 ACT.
2 $9.00 ACT.
3 $5.00 ACT.
4 $5.00 ACT.
5 $3.00 ACT.
Page 8
9. 1 x AccountAccount
Activity
n x Activities
Debt Collection
1. Open Account
2. Call Debtor
3.Send letter to
Debtor
4. Debtor calls
debtee
5. Close Account
Activities
Fee n x Fees
Fees
Stamps
Disbursement
Admin
Legal
Postage
Page 9
14. Do Until(Put(Fee_total, $ACTFEE.)='Activity-End');
*** 1. Loop for each activity *** ;
Iter+1;
If Iter=1 then Fee_total=Put(Cat("A",Put(Activity_ID, z3.)), $ACTFEE.);
Else Fee_total=Put(Fee_total, $ACTFEE.);
*** 2. Apply correct fee per actiovity for the date *** ;
FeePrt=Cat(Strip(Scan(Fee_total,-1,'-')),"-",Put(Asat_Date, z7.));
FeeAmt=Put(FeePrt, $Fee.);
Fees=Sum(Fees, FeeAmt);
Output; Add this line if you want to test the breakdown of fees charged
End;
Account
Activities
Page 14
Formats and How to use them by Patrick Cuba @ Cuba BI Consulting
A format is a type of SAS language element that applies a pattern to or executes instructions for a data value to be displayed or written as output. Types of formats correspond to the type of data: numeric, character, date, time, or timestamp. The ability to create user-defined formats is also supported
Data _Null_;
Text="Hello Bill";
Num=25.50;
Date='25FEB2014'd;
Time='16:45:24't;
DateTime = DHMS(Date, Hour(Time), Minute(Time), Second(Time));
TextMD5=MD5(Text);
Put 'Hex' Text=$Hex.;
Put 'Upcase' Text=$Upcase.;
Put 'Reverse' Text=$Revers20.;
Put 'Rounded number' Num=11.;
Put 'Two delimiter number' Num=11.2;
Put 'Dollar value' Num=DOLLAR10.2;
Put 'Normal Date' Date=Date9.;
Put 'European standard' Date=yymmdd10.;
Put 'Changed delimiter' Date=yymmddp10.;
Put 'Date as numeric' Date=11.;
Put 'Date as numeric string' Date=Z7.;
Put Time=Time8.;
Put Datetime=datetime21.2;
Put 'MD5' TextMD5=;
Put 'Readable MD5' TextMD5=$Hex16.;
Run;
The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number
Informats are used to import the values in a specified format from files, datalines, datasets or external tables
Formats are used to display the underlying data in a SAS format
Using input and puts data can be converted between numeric and character and vice versa
Picture formats give you the ability to create templates for data, much like Excel Text function
Currency conversion on the fly!
In addition to the formats that are supplied with Base SAS software, you can create your own formats. In Base SAS software, PROC FORMAT enables you to create your own formats for both character and numeric variables.
Syntax for PROC FORMAT loading a data lookup table into a SAS catalog
Start = Lookup value
End = Lookup value to range
Label = Lookup value returned
HLO = High/Low/Other
Type = Numeric or character
To check formats created use CNTLOUT=
Account has activities
Each account attracts activities;
Open Account
Calling the debtor
Sending a letter to the debtor
Debtor calling the debtee
Multiple letters, calls
Finally Account Closure
Activity has a fee
Each activity attracts a fee
3. Activity has many fees
4. Fees increase over time
Formats are loaded in memory and applied to one activity at a time
Activity Lookup – Build Format table
~~~~~~~~~~~~~~~~~~~~~~
A1 F1 to A1 F2
A1 F2 to A1 F3
A1 F3 to A1 F6
A1 F6 to A1 F7
Fee Lookup – Build Format table with ranges
~~~~~~~~~~~~~~~~~~~~~~~~~~~
F1-1/1/2014 to F1-28/2/2014 – Fee
F1-1/3/2014 to F1-30/4/2014 – Fee
F1-1/5/2014 to F1-31/12/9999 – Fee
Applies the correct fees over time
On 1/3 there was an admin fee increase of 50c
That is reflected in the Letter activity on 31/3
Simple loop step to apply fees to an activity and against the time of the activity
01_Create Reference Tables.sas creates the lookup tables for use in formats
02_Create Data Table.sas create sample data with test cases
03_Create Formats.sas creates formats from the lookup tables
04_Apply Formats.sas applies the formats created to the sample data
Account-Activities.csv
Activity.csv
Fees.csv