FORMAT FESTIVAL AN INTRODUCTION TO SAS® FORMATS AND INFORMATS By David Maddox.

Post on 24-Dec-2015

220 views 0 download

Tags:

Transcript of FORMAT FESTIVAL AN INTRODUCTION TO SAS® FORMATS AND INFORMATS By David Maddox.

FORMAT FESTIVAL

AN INTRODUCTION TO

SAS® FORMATS AND INFORMATS

By David Maddox

FORMAT FESTIVAL

• Formats and Informats - powerful SAS® language components.

• Format - an instruction or template that SAS uses to output data values.

• Informat - an instruction or template for reading data values.

FORMAT FESTIVAL

• There are two basic types of Informats – SAS® Supplied and User Created.

• Character Informats have the form - $<informat name>w.

• Examples - $char7. , $hex4.

FORMAT FESTIVAL

• Numeric Informats omit the “$” sign and may include a decimal width.

• Examples of numeric Informats are:

comma7.2 – removes embedded commas and $ signs.

pd4. – Reads packed decimal

FORMAT FESTIVAL

• Date Informats are used to convert date and time information into a SAS® date format.

• A SAS date is a number which represents the number of days since January 1, 1960.

• Examples of date Informats are: mmddyy10. – 10/09/2006 date11. – 09/OCT/2006

FORMAT FESTIVAL

Specifying Informats for use in a program.

- INPUT Statement - INPUT, INPUTC, INPUTN Functions - INFORMAT Statement - ATTRIB Statement

FORMAT FESTIVALExample 1 – Using an Informat to read in a

variable.03/01/2002 $1,035,053.03/04/2002 $1,075,310.03/05/2002 $1,236,607.03/06/2002 $1,422,098.03/07/2002 $1,635,413.03/08/2002 $1,880,725.03/11/2002 $2,162,834.03/12/2002 $2,487,259.03/13/2002 $2,860,348.03/14/2002 $3,289,400.03/15/2002 $3,782,810.03/18/2002 $4,350,232.03/19/2002 $5,002,767.

FORMAT FESTIVALdata ex1;

Infile 'c:\Workshop\HW07\Data\ex1.txt;

input datev mmddyy10. revenue comma12.;

run;

FORMAT FESTIVAL

Example 2 – Creating and using a user defined Informat.

- PROC FORMAT is used to create user-defined Formats and

Informats- Use an INFORMAT or ATTRIB

statement to permanently associate a variable and an Informat

FORMAT FESTIVALproc format;

invalue $ganpa '229'='Albany' '706','762'='Athens' '404','470','678','770'='Atlanta'

'478'='Macon' '912'='Savannah';run;

data ex2;length npa $8;input npa $ganpa3. nxx $ 4-6 line $ 7-10 revenue 12.2;cards;404423AAAA50.0229543XXXX75.0912333BBBB25.0912767CCCC31.75678221DDDD46.0478332EEEE34.5770681FFFF21.7706728GGGG45.8229476HHHH31.2

;run;

FORMAT FESTIVALObs npa nxx line revenue

1 Atlanta 423 AAAA 50

2 Albany 543 XXXX 75

3 Savannah 333 BBBB 25

4 Savannah 767 CCCC 31.75

5 Atlanta 221 DDDD 46

6 Macon 332 EEEE 34.5

7 Atlanta 681 FFFF 21.7

8 Athens 728 GGGG 45.8

9 Albany 476 HHHH 31.2

FORMAT FESTIVAL

Example 3 – Creating a permanent user defined Informat

- Reference the catalog using PROC FORMAT Library= option.

- Use LIBNAME to declare a FORMATS catalog

FORMAT FESTIVALproc format library=library; invalue $ganpa '229'='Albany' '706','762'='Athens' '404','470','678','770'='Atlanta' '478'='Macon '912'='Savannah'; run; proc format library=library fmtlib; select @$ganpa; run; The FMTLIB produces the following information about the $ganpa. INFORMAT. This option can be used to check the user created INFORMAT for accuracy and completeness.

FORMAT FESTIVAL

| INFORMAT NAME: @$GANPA LENGTH: 8 NUMBER OF VALUES: 9 MIN LENGTH: 1 MAX LENGTH: 40 DEFAULT LENGTH 8 FUZZ: 0

|-------------------------------------------------------------------------- |START |END |INVALUE(VER. V7|V8 04JUL2006:18:25:37)| |----------------+----------------+---------------------------------------- |229 |229 |Albany |404 |404 |Atlanta |470 |470 |Atlanta |478 |478 |Macon |678 |678 |Atlanta |706 |706 |Athens |762 |762 |Athens |770 |770 |Atlanta |912 |912 |Savannah ----------------------------------------------------------------------------

FORMAT FESTIVAL

Example 4 – Using a Format to print a variable

- PUT Statement - PUT, PUTC, PUTN Functions - %SYSFUNC macro function - FORMAT Statement - ATTRIB Statement

FORMAT FESTIVALlibname ds 'c:\Workshop\HW07\Datasets';

proc print data=ds.ex1;run;

proc print data=ds.ex1;format datev mmddyy10. revenue comma12.;run;

proc print data=ds.ex1;format datev mmddyy7. revenue dollar14.2;run;

proc print data=ds.ex1;format datev date7. revenue euro12.;run;

proc print data=ds.ex1;format datev worddate12. revenue e12.;run;

data _null_;set ds.ex1;file print;put datev date9. +1 revenue comma14.2;run;

FORMAT FESTIVAL

Example 5 – Creating and using a user defined Formats

- PROC FORMAT is used to create user-defined Formats and

Informats- Use an FORMAT or ATTRIB

statement to permanently associate a variable and a Format

FORMAT FESTIVALproc format; value $ganpaf '229'='Albany' '706','762'='Athens' '404','470','678','770'='Atlanta’ '478'='Macon' '912'='Savannah'; run; proc print data=ex2; format npa $ganpaf.; run;

FORMAT FESTIVAL

Obs npa nxx line revenue 1 Atlanta 423 AAAA 50.00 2 Albany 543 XXXX 75.00 3 Savannah 333 BBBB 25.00 4 Savannah 767 CCCC 31.75 5 Atlanta 221 DDDD 46.00 6 Macon 332 EEEE 34.50 7 Atlanta 681 FFFF 21.70 8 Athens 728 GGGG 45.80 9 Albany 476 HHHH 31.20

FORMAT FESTIVAL

Example 6 – Using a FORMAT to classify data.

- Use PROC FORMAT to declare a range instead of just one equivalent value.

- Apply the FORMAT range to the original variable in a PROC or DATA step.

 

FORMAT FESTIVALproc format; value revtype 0 -< 35 = 'LOW' 36 -< 55 = 'AVERAGE' 56 -< 80 = 'HIGH'; run; data ex3; set ex2; revenue1=revenue; run; proc print data=ex3; format revenue revtype.; var npa nxx line revenue1 revenue; Run;

FORMAT FESTIVAL

Obs npa nxx line revenue1 revenue 1 404 423 AAAA 50.00 AVERAGE 2 229 543 XXXX 75.00 HIGH 3 912 333 BBBB 25.00 LOW 4 912 767 CCCC 31.75 LOW 5 678 221 DDDD 46.00 AVERAGE 6 478 332 EEEE 34.50 LOW 7 770 681 FFFF 21.70 LOW 8 706 728 GGGG 45.80 AVERAGE9 229 476 HHHH 31.20 LOW

FORMAT FESTIVAL

Exercise 7 - Using a Format as a look-up facility

- Define a FORMAT using PROC FORMAT

- Use a PUT function to test each observation AND select by the formatted value

FORMAT FESTIVALProc format; value $metro

'404','470','678','770'='me'; run; data metro; set ex2; if put(npa,$metro.)='me'; proc print data=metro; title 'Metro Atlanta'; run;

FORMAT FESTIVAL

Metro Atlanta

Obs npa nxx line revenue 1 404 423 AAAA 50.0 2 678 221 DDDD 46.0 3 770 681 FFFF 21.7

FORMAT FESTIVAL

Example 8 – Creating a Format with the CNTLN option

- Create a data set that can be used to construct a Format-Define variables – START, LABEL, FMTNAME as a minimum.

-Read the data set into PROC FORMAT using the CNTLN option

FORMAT FESTIVAL

FORMAT FESTIVALlibname ds 'J:\Flash_Drive_4_Gig\SESUG 2006\HW07';libname library 'J:\Flash_Drive_4_Gig\SESUG 2006\HW07';

data ocnlist;length label $ 50;set ds.nm_ocn_list;start=ocn;label=carrier;fmtname='$New_Mexico_OCN';run;

proc format library=library cntlin=ocnlist;run;

proc format library=library fmtlib;select $New_Mexico_OCN;run;

FORMAT FESTIVAL

• Traffic LightingNot a recode, but an improvement in the visual effect of making certain values stand out. Example:

Obs npa nxx line revenue1 404 423 AAAA 50.002 229 543 XXXX 75.003 912 333 BBBB 25.004 912 767 CCCC 31.755 678 221 DDDD 46.006 478 332 EEEE 34.507 770 681 FFFF 21.708 706 728 GGGG 45.809 229 476 HHHH 31.20

FORMAT FESTIVAL

• proc format;• value larg low - 24.00 ='Green'• 23.00 - 49.00='Blue'• 50.00 - high='Red';• run;

• proc print data=subscribers;• var npa nxx line;• var revenue / style={foreground=larg.};• run;

FORMAT FESTIVAL

Obs npa nxx line revenue

1 404 423 AAAA 50.00

2 229 543 XXXX 75.00

3 912 333 BBBB 25.00

4 912 767 CCCC 31.75

5 678 221 DDDD 46.00

6 478 332 EEEE 34.50

7 770 681 FFFF 21.70

8 706 728 GGGG 45.80

9 229 476 HHHH 31.20

- The key element is the use of the “style” option on the revenue variable. -This method is supported for PROC PRINT, PROC TABULATE, and PROC REPORT.-“FORGROUND” controls font color.

FORMAT FESTIVAL

Obs npa nxx line revenue

1 404 423 AAAA 50.00

2 229 543 XXXX 75.00

3 912 333 BBBB 25.00

4 912 767 CCCC 31.75

5 678 221 DDDD 46.00

6 478 332 EEEE 34.50

7 770 681 FFFF 21.70

8 706 728 GGGG 45.80

9 229 476 HHHH 31.20

-The “background” attribute controls the cell color.

-In Version 8.2, the presence of a permanent FORMAT for the traffic lighting variable prevented the traffic lighting effect when using PROC PRINT. This problem was fixed in Version 9.1

FORMAT FESTIVAL

• Nested FORMATS

PROC FORMAT; value $metro '404'='City' '470','678','770'='Metro'

'229','912'='Outstate' other=[$inter.];

value $inter '803'='SC' '423'='TN' '205'='AL'

other='Rest of Country';run;

FORMAT FESTIVAL

Obs npa NPA nxx line revenue

1 City 404 423 AAAA 50.00

2 Outstate 229 543 XXXX 75.00

3 Outstate 912 333 BBBB 25.00

4 Outstate 912 767 CCCC 31.75

5 Metro 678 221 DDDD 46.00

6 Rest of Country 478 332 EEEE 34.50

7 Metro 770 681 FFFF 21.70

8 Rest of Country 706 728 GGGG 45.80

9 Outstate 229 476 HHHH 31.20

10 AL 205 768 xxxx 35.00

Using the $metro FORMAT to print the subscribers dataset produces the following output:

FORMAT FESTIVAL

FORMAT FESTIVAL

Version 9.2 New Formats$BASE64X – Converts character data to ASCII text using Base 64 encoding.PERCENTN – Produces percentages, using a minus sign for negative values.DATEW. – Enhanced to write dates in the

form dd-mmm-yyy.

FORMAT FESTIVAL

• This is a very brief look at Formats and Informats.

• They can help you write some very elegant and concise code.

• Make them a permanent part of your SAS® repertoire.

SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.