Sas Export

  • Uploaded by: sarath.annapareddy
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Sas Export as PDF for free.

More details

  • Words: 1,884
  • Pages: 35
SAS Export

Last Updated : 29 June, 2004

Center of Excellence

Objectives  Write observations from a SAS data set to a comma-delimited external file.  Use DATA step logic to insert a header record and a footer record into an external file.

Introduction You can use the DATA step to write  a custom report  data to an external file to be read by other programming languages or software.

Introduction READING FROM AN EXTERNAL FILE

WRITING TO AN EXTERNAL FILE

The DATA statement begins the DATA step.

The DATA statement begins the DATA step.

The INFILE statement identifies an external file to read with an INPUT statement.

The FILE statement identifies an external file to write with a PUT statement.

The INPUT statement describes the arrangement of values in the input data record.

The PUT statement describes the arrangement of values in the output data record.

The DATA Statement Usually, the DATA statement specifies at least one data set name that the SAS System uses to create an output data set. Using the keyword _NULL_ as the data set name causes SAS to execute the DATA step without writing observations to a data set. DATA DATA _NULL_; _NULL_;

The FILE Statement The FILE statement can be used to specify the output destination for subsequent PUT statements. General form of the FILE statement: FILE FILE file-specification file-specification; ;

You can use the FILE statement in conditional processing (IF-THEN/ELSE or SELECT) because it is executable.

The PUT Statement The PUT statement can write lines to the external file that is specified in the most recently executed FILE statement. General form of the PUT statement: PUT PUTvariable-1 variable-1 variable-2 variable-2… … variable-n; variable-n;

With simple list output, you list the names of the variables whose values you want written. The PUT statement writes a variable value, inserts a single blank, and then writes the next value.

Modified List Output Modified list output increases the versatility of the PUT statement because you can specify a SAS format to control how the variable values are written. To use modified list output, use the colon (:) format modifier in the PUT statement between the variable name and the format. PUTvariable-1 variable-1 :: format-1. format-1. .. PUT variable-2 . variable-2:: format-2. format-2.

variable-n variable-n:: format-n.; format-n.;

Writing to an External File The prog2.maysales data set contains information about houses. Read this data set and write the data to an external file. prog2.maysales Description Colonial Townhouse Townhouse Ranch Victorian

List Date 13803 13894 14108 14585 14805

Sell Date 14001 14016 14392 14736 15106

SellPrice 355182.74 241225.17 238135.98 219391.80 358186.78

Writing to an External File data _null_;

set prog2.maysales;

file 'raw-data-file';

Why is the $ omitted after Description in the PUT statement? put Description ListDate : date9.

Writing to an External File Partial SAS Log NOTE: 5 records were written to ‘export.dat'. The minimum record length The maximum record length NOTE: There were 5 observations the data set PROG2.MAYSALES.

the file was 34. was 38. read from

Can you use PROC PRINT to view the raw data file?

The FSLIST Procedure The FSLIST procedure enables you to browse external files that are not SAS data sets within an interactive SAS session. PROC PROCFSLIST FSLISTFILEREF=file-specification FILEREF=file-specification; ; RUN; RUN;

Remember to close the FSLIST window when you have finished browsing your external file.

Reading from an External File proc fslist fileref='raw-data-file'; run; Colonial 16OCT1997 02MAY1998 $355,183 Townhouse 15JAN1998 17MAY1998 $241,225 Townhouse 17AUG1998 28MAY1999 $238,136 Ranch 07DEC1999 06MAY2000 $219,392 Victorian 14JUL2000 11MAY2001 $358,187 How can you add a single row of column headers before the rows of data?

The _N_ Automatic Variable (Review) The _N_ automatic variable is created by every DATA step. Each time the DATA step loops past the DATA statement, _N_ is incremented by 1. Therefore, the value of _N_ represents the number of times the DATA step has iterated. _N_ is added to the program data vector but is not output.

Writing to an External File data _null_;

set prog2.maysales;

file 'raw-data-file'; if _N_=1 then

Why is the second PUT statement contained an ELSE statement? putin'Description ' 'ListDate ' 'SellDate ' 'SellPrice';

not

Writing to an External File

proc fslist fileref='raw-data-file'; run; Description ListDate SellDate SellPrice Colonial 16OCT1997 02MAY1998 $355,183 Townhouse 15JAN1998 17MAY1998 $241,225 Townhouse 17AUG1998 28MAY1999 $238,136 Ranch 07DEC1999 06MAY2000 $219,392 Victorian 14JUL2000 11MAY2001 $358,187

How can you add a footer record after the rows of data?

The END= Option in the SET Statement The END= option in the SET statement creates and names a temporary variable that acts as an end-offile indicator.

SET SET SAS-data-set SAS-data-set END=variable END=variable; ; The temporary variable, which is initialized to 0, is set to 1 when the SET statement reads the last observation of the data set listed. The variable is not added to any new data set.

Writing to an External File  data _null_;

set prog2.maysales end=IsLast;

file 'raw-data-file'; if _N_=1 then

put 'Description ' 'ListDate ' 'SellDate ' 'SellPrice';

Execute

data _null_; set prog2.maysales end=IsLast; Partial Listing of prog2.maysales file 'raw-data-file'; if _N_=1 then List Sell put 'Description ' 'ListDate ' Description Date Date 'SellDate ' 'SellPrice'; put Description Colonial 13803 14001 ListDate : date9. Townhouse 13894 14016 SellDate : date9. Townhouse 14108 14392 SellPrice : dollar8.; Ranch 14585 14736 if IsLast=1 then Victorian 14805 15106 put 'Data: PROG2.MAYSALES'; run;

PDV LIST ISLAST DESCRIPTION DATE $ N D N 8 9 8

SELL DATE N 8

SELL PRICE N 8

_N_ D N 8 ...

data _null_; set prog2.maysales end=IsLast; Partial Listing of prog2.maysales file 'raw-data-file'; if _N_=1 then List Sell put 'Description ' 'ListDate ' Description Date Date 'SellDate ' 'SellPrice'; put Description Colonial 13803 14001 ListDate : date9. Townhouse 13894 14016 SellDate : date9. Townhouse 14108 14392 SellPrice : dollar8.; Ranch 14585 14736 if IsLast=1 then Victorian 14805 15106 put 'Data: PROG2.MAYSALES'; run;

PDV LIST ISLAST DESCRIPTION DATE $ N D N 8 9 8

0

.

SELL DATE N 8

SELL PRICE N 8

.

.

_N_ D N 8

1 ...

data _null_; set prog2.maysales end=IsLast; Partial Listing of prog2.maysales file 'raw-data-file'; if _N_=1 then List Sell put 'Description ' 'ListDate ' Description Date Date 'SellDate ' 'SellPrice'; put Description Colonial 13803 14001 ListDate : date9. Townhouse 13894 14016 SellDate : date9. Townhouse 14108 14392 SellPrice : dollar8.; Ranch 14585 14736 if IsLast=1 then Victorian 14805 15106 put 'Data: PROG2.MAYSALES'; run;

PDV LIST ISLAST DESCRIPTION DATE $ N D N 8 9 8

0

Colonial

13803

SELL DATE N 8

SELL PRICE N 8

14001

355182

_N_ D N 8

1

... ...

data _null_; set prog2.maysales end=IsLast; Partial Listing of prog2.maysales file 'raw-data-file'; if _N_=1 then List Sell put 'Description ' 'ListDate ' Description Date Date 'SellDate ' 'SellPrice'; put Description Colonial 13803 14001 ListDate : date9. Townhouse 13894 14016 SellDate : date9. Townhouse 14108 14392 SellPrice : dollar8.; Ranch 14585 14736 if IsLast=1 then Victorian 14805 15106 put 'Data: PROG2.MAYSALES'; run;

PDV LIST ISLAST DESCRIPTION DATE $ N D N 8 9 8

0

Colonial

13803

SELL DATE N 8

SELL PRICE N 8

14001

355182

_N_ D N 8

1

... ...

data _null_; set prog2.maysales end=IsLast; Partial Listing of prog2.maysales file 'raw-data-file'; if _N_=1 then List Sell put 'Description ' 'ListDate ' Description Date Date 'SellDate ' 'SellPrice'; put Description Colonial 13803 14001 ListDate : date9. Townhouse 13894 14016 SellDate : date9. Townhouse 14108 14392 SellPrice : dollar8.; Ranch 14585 14736 if IsLast=1 then Victorian 14805 15106 put 'Data: PROG2.MAYSALES'; Continue executing DATA run;

step. _N_ is equal to 1. Write header record to raw-data-file.

PDV

LIST ISLAST DESCRIPTION DATE $ N D N 8 9 8

0

Colonial

13803

SELL DATE N 8

SELL PRICE N 8

14001

355182

_N_ D N 8

1

... ...

data _null_; set prog2.maysales end=IsLast; Partial Listing of prog2.maysales file 'raw-data-file'; if _N_=1 then List Sell put 'Description ' 'ListDate ' Description Date Date 'SellDate ' 'SellPrice'; put Description Colonial 13803 14001 ListDate : date9. Townhouse 13894 14016 SellDate : date9. Townhouse 14108 14392 SellPrice : dollar8.; Ranch 14585 14736 if IsLast=1 then Victorian 14805 15106 put 'Data: PROG2.MAYSALES'; run; IsLast is equal to 0.

PUT statement is not executed.

PDV

LIST ISLAST DESCRIPTION DATE $ N D N 8 9 8

0

Colonial

13803

SELL DATE N 8

SELL PRICE N 8

14001

355182

_N_ D N 8

1

... ...

data _null_; set prog2.maysales end=IsLast; Partial Listing of prog2.maysales file 'raw-data-file'; if _N_=1 then List Sell put 'Description ' 'ListDate ' Description Date Date 'SellDate ' 'SellPrice'; put Description Colonial 13803 14001 ListDate : date9. Townhouse 13894 14016 SellDate : date9. Townhouse 14108 14392 SellPrice : dollar8.; Ranch 14585 14736 if IsLast=1 then Victorian 14805 15106 put 'Data: PROG2.MAYSALES'; run; IsLast is equal to 0.

PUT statement is not executed.

PDV

LIST ISLAST DESCRIPTION DATE $ N D N 8 9 8

0

Townhome

13894

SELL DATE N 8

SELL PRICE N 8

14108

241225

_N_ D N 8

2

... ...

data _null_; set prog2.maysales end=IsLast; Partial Listing of prog2.maysales file 'raw-data-file'; if _N_=1 then List Sell put 'Description ' 'ListDate ' Description Date Date 'SellDate ' 'SellPrice'; put Description Colonial 13803 14001 ListDate : date9. Townhouse 13894 14016 SellDate : date9. Townhouse 14108 14392 SellPrice : dollar8.; Ranch 14585 14736 if IsLast=1 then Victorian 14805 15106 put 'Data: PROG2.MAYSALES'; run; IsLast is equal to 1.

PUT statement is PDV executed. Write footer record to raw-data-file. LIST SELL ISLAST DESCRIPTION DATE DATE $ N N D N 8 9 8 8

1

Victorian 14805

15106

SELL PRICE N 8

358156

_N_ D N 8

5

... ...

Writing to an External File proc fslist fileref='raw-data-file'; run; Description ListDate SellDate SellPrice Colonial 16OCT1997 02MAY1998 $355,183 Townhouse 15JAN1998 17MAY1998 $241,225 Townhouse 17AUG1998 28MAY1999 $238,136 Ranch 07DEC1999 06MAY2000 $219,392 Victorian 14JUL2000 11MAY2001 $358,187 Data: PROG2.MAYSALES

Specifying an Alternate Delimiter Use the DLM= option in the FILE statement to create a file with an alternate delimiter (other than a blank). FILE FILE file-specification file-specificationDLM='quoted-string' DLM='quoted-string' ; ;

You can also specify a character variable whose value contains your delimiter, instead of a quoted string.

Writing to an External File  data _null_;

set prog2.maysales end=IsLast;

file 'raw-data-file' dlm=','; if _N_=1 then

put 'Description,ListDate,' 'SellDate,SellPrice';

Writing to an External File proc fslist fileref='raw-data-file'; run; Description,ListDate,SellDate,SellPrice Colonial,16OCT1997,02MAY1998,$355,183 Townhouse,15JAN1998,17MAY1998,$241,225 Townhouse,17AUG1998,28MAY1999,$238,136 Ranch,07DEC1999,06MAY2000,$219,392 Victorian,14JUL2000,11MAY2001,$358,187 Data: PROG2.MAYSALES

What is the role of each comma in the records containing data?

Embedded Delimiters These commas act as delimiters. The DLM= option in the FILE statement causes their appearance. 

Partial Output

Colonial,16OCT1997,02MAY1998,$355,183 Colonial,16OCT1997,02MAY1998,$355,183

This comma acts as part of a SAS format. The DOLLAR8. format in the PUT statement causes its appearance. How many data values are contained in each record? How many comma-delimited fields are in each record? ... ...

Accounting for Embedded Delimiters Use the DSD option in the FILE statement to write data items that contain embedded delimiters. FILE FILEfile-specification file-specification DLM='quoted-string' DLM='quoted-string'DSD DSD ; ;

Any data item that contains the specified delimiter is quoted with the double quotation mark (") when the data item is output.

Writing to an External File  data _null_;

set prog2.maysales;

file 'raw-data-file' dlm=',' dsd; put Description ListDate : date9. SellDate : date9. SellPrice : dollar8.; run;

Writing to an External File proc fslist fileref='raw-data-file'; run; Colonial,16OCT1997,02MAY1998,"$355,183" Townhouse,15JAN1998,17MAY1998,"$241,225" Townhouse,17AUG1998,28MAY1999,"$238,136" Ranch,07DEC1999,06MAY2000,"$219,392" Victorian,14JUL2000,11MAY2001,"$358,187"

Questions

Related Documents

Sas Export
June 2020 10
Export+
October 2019 47
Sas
April 2020 20
Sas
June 2020 17
Export
November 2019 40
Sas
November 2019 31