Docsity

SAS Programming: Creating Customized Reports using FILE and PUT Statements - Prof. A. John, Study notes of Statistics

Toggle sidebar
bg1
Week 09 Class Activities
File: week-09-19oct05.doc
Directory (hp/compaq):
C:\baileraj\Classes\Fall 2005\sta402\handouts
C:\Documents and Settings\John Bailer\My Documents\baileraj\
Classes\Fall 2003\sta402\handouts\week9-20oct03.doc
WRITING EXTERNAL FILES
* Statements: FILE, PUT
* Using DATA _NULL_
* PUT function
* Creating customized reports using DATA steps
TRANSPORTING SAS DATA SETS AND CATALOGS BETWEEN SYSTEMS
* PROC CPORT
* PROC CIMPORT
References:
Excerpts from SAS Online Documentation and system help.
DATA _NULL_
* can use a DATA step to generate a report without creating a data set by using _NULL_ in the
DATA statement.
* saves system resources because SAS does not create a data set.
FILE and PUT statements
DATA Step Statements: Syntax
FILE FILE file-specification < options> < host-options>;
DATA Step Statements: FILE Statement - Options
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download SAS Programming: Creating Customized Reports using FILE and PUT Statements - Prof. A. John and more Study notes Statistics in PDF only on Docsity!

Week 09 Class Activities

File: week-09-19oct05.doc

Directory (hp/compaq):

C:\baileraj\Classes\Fall 2005\sta402\handouts

C:\Documents and Settings\John Bailer\My Documents\baileraj\

Classes\Fall 2003\sta402\handouts\week9-20oct03.doc

WRITING EXTERNAL FILES

* Statements: FILE, PUT

* Using DATA NULL

* PUT function

* Creating customized reports using DATA steps

TRANSPORTING SAS DATA SETS AND CATALOGS BETWEEN SYSTEMS

* PROC CPORT

* PROC CIMPORT

References:

Excerpts from SAS Online Documentation and system help.

DATA NULL

* can use a DATA step to generate a report without creating a data set by using NULL in the

DATA statement.

* saves system resources because SAS does not create a data set.

FILE and PUT statements

DATA Step Statements: Syntax

FILE FILE file-specification < options> < host-options>;

DATA Step Statements: FILE Statement - Options

You can use the following options with the FILE statement:

BLKSIZE COLUMN DELIMITER DROPOVER DSD FILENAME FILEVAR

FLOWOVER FOOTNOTES | NOFOOTNOTES HEADER LINE LINESIZE

LINESLEFT LRECL MOD N NBYTE ODS OLD PAD | NOPAD

PAGESIZE PRINT | NOPRINT RECFM STOPOVER

TITLES | NOTITLES FILE

FILE – specify current output file for PUT statements

FILE file-specification < option-list > < host-option-list >;

file-specification

can be any of the file specification forms discussed in "Referencing External Files" in the

"Using External Files" section in SAS Companion for the Microsoft Windows

Environment.

Note: The words CON, NUL, PRN, LPT1 - LPT9, and COM1 - COM9 are reserved

words under Windows. Do not use them as file names.

The FILE statement routes the output from the PUT statement to either the same external file to

which procedure output is written or to a different external file.

DATA Step Statements: PUT Statement

specification

Specifies what is written, how it is written, and where it is written. This can include:

variable names the variable whose value is written.

(variable-list) specifies a list of variables whose values are written.

'character string' specifies a string of text, enclosed in quotation marks, to write.

n* specifies to repeat n times the subsequent character string

pointer control moves the output pointer to a specified line or column in the output buffer.

column-

specifications

specifies to which columns of the output line the values are written.

animal=49 conc=310 brood1=6 brood2=0 brood3=0 total=6 cbrood3=none ERROR= N=

animal=50 conc=310 brood1=5 brood2=0 brood3=0 total=5 cbrood3=none ERROR= N= proc sort data=class.nitrofen; by conc; data NULL; set class.nitrofen; by conc; file 'D:\baileraj\Classes\Fall 2003\sta402\data\week9-put2.lst'; /* Write out header text information for the first observation in each conc*/ if FIRST.conc then do; put 5 '+-----' ‘+’; put 'Conc = ' conc; put @1 ‘Brood 1’ @10 ‘Brood 2’ @20 ‘Brood 3’ @30 ‘TOTAL’; end; / Write out the data for all records / put @ 1 brood1 @ 10 brood2 @ 20 brood3 @ 30 total; run ; data NULL; / add last line to the output file */ file 'D:\baileraj\Classes\Fall 2003\sta402\data\week9-put2.lst' MOD; put 5 *'+-----' ‘+’; run;

*** insert file ***

Conc = 0 Brood 1 Brood 2 Brood 3 TOTAL 3 14 10 27 5 12 15 32 6 11 17 34 6 12 15 33 6 15 15 36 5 14 15 34 6 12 15 33 5 13 12 30 3 10 11 24 6 11 14 31 +-----+-----+-----+-----+-----+ Conc = 80 Brood 1 Brood 2 Brood 3 TOTAL 6 11 16 33 5 12 16 33 6 11 18 35 5 12 16 33 8 13 15 36 3 9 14 26

Conc = 160 Brood 1 Brood 2 Brood 3 TOTAL 6 12 11 29 6 12 11 29 2 8 13 23 6 10 11 27 6 11 13 30 6 13 12 31 6 12 12 30 5 10 11 26 6 13 10 29 6 12 11 29 +-----+-----+-----+-----+-----+ Conc = 235 Brood 1 Brood 2 Brood 3 TOTAL 4 13 6 23 6 10 5 21 2 5 0 7 6 0 6 12 6 13 8 27 6 0 10 16 7 0 6 13 4 2 9 15 6 8 7 21 7 0 10 17 +-----+-----+-----+-----+-----+ Conc = 310 Brood 1 Brood 2 Brood 3 TOTAL 6 0 0 6 6 0 0 6 7 0 0 7 0 0 0 0 5 10 0 15 5 0 0 5 6 0 0 6 4 0 0 4 6 0 0 6 5 0 0 5 +-----+-----+-----+-----+-----+

Note: The PUT statement without arguments is called a null PUT statement. The null PUT

statement writes the current output line to the current file, even if the current output line is blank,

or releases an output line being held by a previous PUT statement with a trailing @.

PUT PUT < specification(s)> <@ | @@>;

put @ 1 brood1 @ 10 brood2 @ 20 brood3 @ 30 total; run ; data NULL; /* add last line to the output file */ file 'D:\baileraj\Classes\Fall 2003\sta402\data\week9-put3.lst' MOD; put 6 *'+-----' ‘+’ // ‘Data from C. dubia Nitrofen exposure data [Bailer & Oris 1993]’; run;

*** insert file ***

Conc = 0 Brood 1 Brood 2 Brood 3 TOTAL

3 14 10 27 5 12 15 32 6 11 17 34 6 12 15 33 6 15 15 36 5 14 15 34 6 12 15 33 5 13 12 30 3 10 11 24 6 11 14 31 +-----+-----+-----+-----+-----+-----+ Conc = 80 Brood 1 Brood 2 Brood 3 TOTAL

6 11 16 33 5 12 16 33 6 11 18 35 5 12 16 33 8 13 15 36 3 9 14 26 5 9 13 27 7 12 12 31 5 13 14 32 3 12 14 29 +-----+-----+-----+-----+-----+-----+ Conc = 160 Brood 1 Brood 2 Brood 3 TOTAL

6 12 11 29 6 12 11 29 2 8 13 23 6 10 11 27 6 11 13 30 6 13 12 31 6 12 12 30 5 10 11 26 6 13 10 29 6 12 11 29 +-----+-----+-----+-----+-----+-----+ Conc = 235 Brood 1 Brood 2 Brood 3 TOTAL

Conc = 310 Brood 1 Brood 2 Brood 3 TOTAL

6 0 0 6 6 0 0 6 7 0 0 7 0 0 0 0 5 10 0 15 5 0 0 5 6 0 0 6 4 0 0 4 6 0 0 6 5 0 0 5 +-----+-----+-----+-----+-----+-----+ Data from C. dubia Nitrofen exposure data [Bailer & Oris 1993]

PUT,

Column

PUT <$> start-column <-end-column><.decimalplaces> <@ |

The PUT, Column Statement writes variable values in the specified columns in the output line.

The following arguments are allowed:

variable names the variable whose value is written.

$ indicates that the variable contains character values rather than numeric values.

start-

column

specifies the first column of the field where the value is written in the output line.

-end--

column

specifies the last column of the field for the value.

.decimal-

places

specifies the number of digits to the right of the decimal point in a numeric value.

If you specify 0 for d or omit d , the value is written without a decimal point.

@ | @@ holds an output line for the execution of the next PUT statement even across

pointer-control specifies one of these pointer controls to use to position a value: @, #, /, +, and

OVERPRINT.

character-string specifies one or more characters to place between formatted values.

n* specifies to repeat n times the next format in a format list.

proc sort data=class.nitrofen; by conc; data NULL; set class.nitrofen; by conc; file 'D:\baileraj\Classes\Fall 2003\sta402\data\week9-put4.lst'; /* Write out header text information for the first observation in each conc*/ if FIRST.conc then do; put 6 '+-----' '+'; put 'Conc = ' conc / @ 1 'Brood 1' @ 10 'Brood 2' @ 20 'Brood 3' @ 30 'TOTAL' / @ 1 '-------' @ 10 '-------' @ 20 '-------' @ 30 '-----'; end; / Write out the data for all records / put @ 5 brood1 3. @ 14 brood2 3. @ 24 brood3 3. @ 32 total 3. ; data NULL; / add last line to the output file */ file 'D:\baileraj\Classes\Fall 2003\sta402\data\week9-put4.lst' MOD; put 6 *'+-----' ‘+’ // ‘Data from C. dubia Nitrofen exposure data [Bailer & Oris 1993]’; run; run ;

*** insert file ***

Conc = 0 Brood 1 Brood 2 Brood 3 TOTAL

3 14 10 27 5 12 15 32 6 11 17 34 6 12 15 33 6 15 15 36 5 14 15 34 6 12 15 33 5 13 12 30 3 10 11 24 6 11 14 31 +-----+-----+-----+-----+-----+-----+ Conc = 80 Brood 1 Brood 2 Brood 3 TOTAL

6 11 16 33 5 12 16 33 6 11 18 35 5 12 16 33

Conc = 160 Brood 1 Brood 2 Brood 3 TOTAL

6 12 11 29 6 12 11 29 2 8 13 23 6 10 11 27 6 11 13 30 6 13 12 31 6 12 12 30 5 10 11 26 6 13 10 29 6 12 11 29 +-----+-----+-----+-----+-----+-----+ Conc = 235 Brood 1 Brood 2 Brood 3 TOTAL

4 13 6 23 6 10 5 21 2 5 0 7 6 0 6 12 6 13 8 27 6 0 10 16 7 0 6 13 4 2 9 15 6 8 7 21 7 0 10 17 +-----+-----+-----+-----+-----+-----+ Conc = 310 Brood 1 Brood 2 Brood 3 TOTAL

6 0 0 6 6 0 0 6 7 0 0 7 0 0 0 0 5 10 0 15 5 0 0 5 6 0 0 6 4 0 0 4 6 0 0 6 5 0 0 5 +-----+-----+-----+-----+-----+-----+ Data from C. dubia Nitrofen exposure data [Bailer & Oris 1993]

PUT,

List

PUT variable <$><@ | @@>;

The following arguments are allowed:

variable names the variable whose value is written.

$ indicates that the variable contains character values rather than numeric values.

format. specifies a format to use when the variable values are written.

start-

column

specifies the first column of the field where the variable name, equal sign, and

value are to be written in the output line.

--end-

column

specifies the last column of the field for the value.

.decimal-

places

specifies the number of digits to the right of the decimal point in a numeric value.

If you specify 0 for d or omit d , the value is written without a decimal point.

@ | @@ holds an output line for the execution of the next PUT statement even across

iterations of the DATA step. These line-hold specifiers are called trailing @ and

double trailing @.

Moving SAS catalogs/data between systems

libname source ' SAS-data-library ';

filename tranfile ' transport-file '

host-option(s)-for-file-characteristics ;

proc cport library=source file=tranfile memtype=catalog;

run;

/* EXPORTING Transport file

  • program executed in SAS on Win2k machine
  • transferring all data files, formats, graphs from

my laptop, Win2k machine, to my unix account

libname class ' D:\baileraj\Classes\Fall 2003\sta402\data’;

filename tran ‘ D:\baileraj\Classes\Fall 2003\sta402\data\sta402-tran.d’;

proc cport library=class file=tran memtype=all; run; on the SAS LOG file we see... NOTE: Proc CPORT begins to transport catalog CLASS.EXCAT NOTE: The catalog has 1 entries and its maximum logical record length is 4076. NOTE: Entry TEMPLATE.GRSEG has been transported. NOTE: Proc CPORT begins to transport catalog CLASS.GRAPH NOTE: The catalog has 5 entries and its maximum logical record length is 4076. NOTE: Entry GPLOT.GRSEG has been transported. NOTE: Entry GPLOT1.GRSEG has been transported. NOTE: Entry GPLOT2.GRSEG has been transported. NOTE: Entry GPLOT3.GRSEG has been transported. NOTE: Entry GSLIDE.GRSEG has been transported. NOTE: Proc CPORT begins to transport data set CLASS.MANATEE NOTE: The data set contains 3 variables and 14 observations. Logical record length is 24. NOTE: Proc CPORT begins to transport data set CLASS.MEAT NOTE: The data set contains 6 variables and 12 observations. Logical record length is 48. NOTE: Proc CPORT begins to transport data set CLASS.NITROFEN NOTE: The data set contains 7 variables and 50 observations. Logical record length is 56. NOTE: Proc CPORT begins to transport catalog CLASS.TEMPCAT NOTE: The catalog has 1 entries and its maximum logical record length is 88. NOTE: Entry NEWTEMP.TEMPLATE has been transported. NOTE: PROCEDURE CPORT used: real time 0.75 seconds

NOTE: SAS (r) Proprietary Software Release 8.1 (TS1M0) Licensed to MIAMI UNIVERSITY, Site 0001295003. NOTE: This session is executing on the SunOS 5.8 platform. This message is contained in the SAS news file, and is presented upon initialization. Edit the files "news" in the "misc/base" directory to display site-specific news and information in the program log. The command line option "-nonews" will prevent this display. NOTE: SAS initialization used: real time 0.17 seconds cpu time 0.07 seconds 1 /* 2 File: week9-import.sas 3 Directory: /export/home/baileraj/sta 4 5 Transport file: sta402-tran.d 6 Transferred to unixgen.muohio.edu via FTP 7 8 IMPORTING Transport file 9 - program executed in SAS on unixgen.muohio.edu machine 10 - transferring all data files, formats, graphs from 11 my laptop, Win2k machine, to my unix account 12 / 13 14 / notice the unix convention for folders */ 15 16 options ls=74 nocenter nodate formdlim="-"; 17 18 libname newclass '/export/home/baileraj/sta402'; NOTE: Libref NEWCLASS was successfully assigned as follows: Engine: V Physical Name: /export/home/baileraj/sta 19 20 filename tran '/export/home/baileraj/sta402/sta402-tran.d'; 21 22 proc cimport library=newclass infile=tran; 23 run; NOTE: Proc CIMPORT begins to create/update catalog NEWCLASS.EXCAT NOTE: TEMPLATE not imported. TEMPLATE already exists on the catalog as TEMPLATE. NOTE: GPLOT not imported. GPLOT already exists on the catalog as GPLOT. NOTE: GPLOT1 not imported. GPLOT1 already exists on the catalog as GPLOT1. NOTE: GPLOT2 not imported. GPLOT2 already exists on the catalog as GPLOT2. NOTE: GPLOT3 not imported. GPLOT3 already exists on the catalog as GPLOT3. NOTE: Total number of entries processed in catalog NEWCLASS.EXCAT: 0 NOTE: Proc CIMPORT begins to create/update catalog NEWCLASS.GRAPH NOTE: GPLOT not imported. GPLOT already exists on the catalog as GPLOT. NOTE: GPLOT1 not imported. GPLOT1 already exists on the catalog as GPLOT1.

2 The SAS System NOTE: GPLOT2 not imported. GPLOT2 already exists on the catalog as GPLOT2. NOTE: GPLOT3 not imported. GPLOT3 already exists on the catalog as GPLOT3. NOTE: GSLIDE not imported. GSLIDE already exists on the catalog as GSLIDE. NOTE: Total number of entries processed in catalog NEWCLASS.GRAPH: 0 NOTE: Proc CIMPORT begins to create/update data set NEWCLASS.MANATEE NOTE: Data set contains 3 variables and 14 observations. Logical record length is 24 NOTE: Proc CIMPORT begins to create/update data set NEWCLASS.MEAT NOTE: Data set contains 6 variables and 12 observations. Logical record length is 48 NOTE: Proc CIMPORT begins to create/update data set NEWCLASS.NITROFEN NOTE: Data set contains 7 variables and 50 observations. Logical record length is 56 NOTE: Proc CIMPORT begins to create/update catalog NEWCLASS.TEMPCAT NOTE: Entry NEWTEMP.TEMPLATE has been imported. NOTE: Total number of entries processed in catalog NEWCLASS.TEMPCAT: 1 NOTE: PROCEDURE CIMPORT used: real time 0.34 seconds cpu time 0.07 seconds 24 25 proc contents data=newclass.all; 26 run; NOTE: PROCEDURE CONTENTS used: real time 0.13 seconds cpu time 0.06 seconds NOTE: The PROCEDURE CONTENTS printed pages 1-4. 27 28 NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513- NOTE: The SAS System used: real time 0.70 seconds cpu time 0.24 seconds /export/home/baileraj/sta402/week9-import.lst The SAS System 1 The CONTENTS Procedure -----Directory-----

3 manatees Num 8 16 2 nboats Num 8 8 1 year Num 8 0

The SAS System 3 The CONTENTS Procedure Data Set Name: NEWCLASS.MEAT Observations: 12 Member Type: DATA Variables: 6 Engine: V8 Indexes: 0 Created: 14:45 Monday, October 20, 2003 Observation Length: 48 Last Modified: 14:45 Monday, October 20, 2003 Deleted Observations: 0 Protection: Compressed: NO Data Set Type: Sorted: NO Label: -----Engine/Host Dependent Information----- Data Set Page Size: 8192 Number of Data Set Pages: 1 First Data Page: 1 Max Obs per Page: 169 Obs in First Data Page: 12 Number of Data Set Repairs: 0 File Name: /export/home/baileraj/sta402/meat.sas7bdat Release Created: 8.0101M Host Created: SunOS Inode Number: 293071 Access Permission: rw------- Owner Name: baileraj File Size (bytes): 16384 -----Alphabetic List of Variables and Attributes-----

Variable Type Len Pos

6 ICo2 Num 8 32 5 IMixed Num 8 24 3 IPlastic Num 8 8 4 IVacuum Num 8 16 1 condition Char 8 40 2 logcount Num 8 0

The SAS System 4 The CONTENTS Procedure Data Set Name: NEWCLASS.NITROFEN Observations: 50 Member Type: DATA Variables: 7 Engine: V8 Indexes: 0 Created: 14:45 Monday, October 20, 2003 Observation Length: 56 Last Modified: 14:45 Monday, October 20, 2003 Deleted Observations: 0

Protection: Compressed: NO Data Set Type: Sorted: YES Label: -----Engine/Host Dependent Information----- Data Set Page Size: 8192 Number of Data Set Pages: 1 First Data Page: 1 Max Obs per Page: 145 Obs in First Data Page: 50 Number of Data Set Repairs: 0 File Name: /export/home/baileraj/sta402/nitrofen.sas7bdat Release Created: 8.0101M Host Created: SunOS Inode Number: 293072 Access Permission: rw------- Owner Name: baileraj File Size (bytes): 16384 -----Alphabetic List of Variables and Attributes-----

Variable Type Len Pos Label

1 animal Num 8 0 animal ID number 3 brood1 Num 8 16 number of young in first brood 4 brood2 Num 8 24 number of young in 2nd brood 5 brood3 Num 8 32 number of young in 3rd brood 7 cbrood3 Char 4 48 2 conc Num 8 8 Nitrofen concentration 6 total Num 8 40 -----Sort Information----- Sortedby: conc Validated: YES Character Set: ANSI