SAS Export

download SAS Export

of 35

Transcript of SAS Export

  • 8/14/2019 SAS Export

    1/35

    SAS ExportSAS Export

    Last Updated : 29 June, 2004

    Center of Excellence

  • 8/14/2019 SAS Export

    2/35

    ObjectivesObjectives

    Write observations from a SAS data set toacomma-delimited external file.

    Use DATA step logic to insert a headerrecord and a footer record into anexternal file.

  • 8/14/2019 SAS Export

    3/35

    IntroductionIntroduction

    You can use the DATA step to writea custom reportdata to an external file to be read by otherprogramming languages or software.

  • 8/14/2019 SAS Export

    4/35

    IntroductionIntroduction

    The INPUT statement describes thearrangement of values in the input

    data record.

    The INFILE statement identifies anexternal file to read with an INPUT

    statement.

    The DATA statementbegins the DATA step.

    READING FROMAN EXTERNAL FILE

    The PUT statement describes thearrangement of values in the output

    data record.

    The FILE statement identifies anexternal file to write with a PUT

    statement.

    The DATA statementbegins the DATA step.

    WRITING TOAN EXTERNAL FILE

  • 8/14/2019 SAS Export

    5/35

    The DATA StatementThe DATA Statement

    Usually, the DATA statement specifies atleast one data set name that the SASSystem uses to create an output data set.Using the keyword _NULL_ as the data setname causes SAS to execute the DATAstep without writing observations to adata set.

    DATA _NULL_;

    DATA _NULL_;

  • 8/14/2019 SAS Export

    6/35

    The FILE StatementThe FILE Statement

    The FILE statement can be used to specifythe output destination for subsequent PUTstatements.

    General form of the FILE statement:

    You can use the FILE statement inconditional processing (IF-THEN/ELSE orSELECT) because it is executable.

    FILE file-specification ;

    FILE file-specification ;

  • 8/14/2019 SAS Export

    7/35

    The PUT StatementThe PUT Statement

    The PUT statement can write lines to theexternal file that is specified in the mostrecently executed FILE statement.General form of the PUT statement:

    With simple list output, you list the namesof the variables whose values you wantwritten. The PUT statement writes avariable value, inserts a single blank, andthen writes the next value.

    PUT variable-1 variable-2 variable-n ;PUT variable-1 variable-2 variable-n ;

  • 8/14/2019 SAS Export

    8/35

    Modified List OutputModified List Output

    Modified list output increases theversatility of the PUT statement becauseyou can specify a SAS format to controlhow the variable values are written.

    To use modified list output, use the colon(:) format modifier in the PUT statementbetween the variable name and theformat.

    PUT variable-1 : format-1.variable-2 : format-2.

    variable-n : format-n. ;

    PUT variable-1 : format-1.variable-2 : format-2.

    variable-n : format-n. ;

    ...

  • 8/14/2019 SAS Export

    9/35

    Writing to an External FileWriting to an External File

    The prog2.maysales data set containsinformation about houses. Read this dataset and write the data to an external file.

    List SellDescription Date Date SellPrice

    Colonial 13803 14001 355182.74Townhouse 13894 14016 241225.17Townhouse 14108 14392 238135.98Ranch 14585 14736 219391.80Victorian 14805 15106 358186.78

    prog2.maysales

  • 8/14/2019 SAS Export

    10/35

    Writing to an External FileWriting to an External File

    data _null_;

    set prog2.maysales;

    file ' raw-data-file ';

    put DescriptionListDate : date9.

    Why is the $ omitted after Description in thePUT statement?

  • 8/14/2019 SAS Export

    11/35

    Writing to an External FileWriting to an External File

    Partial SAS Log

    Can you use PROC PRINT to view the rawdata file?

    NOTE: 5 records were written to the fileexport.dat'.

    The minimum record length was 34.The maximum record length was 38.NOTE: There were 5 observations read fromthe data set PROG2.MAYSALES.

  • 8/14/2019 SAS Export

    12/35

    The FSLIST ProcedureThe FSLIST Procedure

    The FSLIST procedure enables you tobrowse external files that are not SAS datasets within an interactive SAS session.

    Remember to close the FSLIST windowwhen you have finished browsing yourexternal file.

    PROC FSLIST FILEREF= file-specification ;RUN ;

    PROC FSLIST FILEREF= file-specification ;RUN ;

  • 8/14/2019 SAS Export

    13/35

    Reading from an External FileReading from an External File

    How can you add a single row of column headersbefore the rows of data?

    proc fslist fileref= 'raw-data-file' ;run;

    Colonial 16OCT1997 02MAY1998 $355,183Townhouse 15JAN1998 17MAY1998 $241,225Townhouse 17AUG1998 28MAY1999 $238,136Ranch 07DEC1999 06MAY2000 $219,392Victorian 14JUL2000 11MAY2001 $358,187

  • 8/14/2019 SAS Export

    14/35

    The _N_ Automatic Variable(Review)The _N_ Automatic Variable(Review)

    The _N_ automatic variable is created byevery DATA step.Each time the DATA step loops past theDATA statement, _N_ is incremented by 1.Therefore, the value of _N_ represents thenumber of times the DATA step hasiterated.

    _N_ is added to the program data vectorbut is not output.

  • 8/14/2019 SAS Export

    15/35

    Writing to an External FileWriting to an External File

    data _null_;

    set prog2.maysales;

    file ' raw-data-file ';

    if _N_=1 then

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

    Why is the second PUT statement notcontained in an ELSE statement?

  • 8/14/2019 SAS Export

    16/35

    Writing to an External FileWriting to an External File

    How can you add a footer record after the

    rows of data?

    proc fslist fileref=' raw-data-file ';run;

    Description ListDate SellDate SellPriceColonial 16OCT1997 02MAY1998 $355,183Townhouse 15JAN1998 17MAY1998 $241,225Townhouse 17AUG1998 28MAY1999 $238,136

    Ranch 07DEC1999 06MAY2000 $219,392Victorian 14JUL2000 11MAY2001 $358,187

  • 8/14/2019 SAS Export

    17/35

    The END= Option in the SETStatementThe END= Option in the SETStatement

    The END= option in the SET statement creates andnames a temporary variable that acts as an end-of-file indicator.

    The temporary variable, which is initialized to 0, isset to 1 when the SET statement reads the lastobservation of the data set listed.The variable is not added to any new data set.

    SET SAS-data-set END=variable ;SET SAS-data-set END=variable ;

  • 8/14/2019 SAS Export

    18/35

    Writing to an External FileWriting to an External File

    data _null_;

    set prog2.maysales end=IsLast;

    file ' raw-data-file ';

    if _N_=1 then

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

  • 8/14/2019 SAS Export

    19/35...

    PDV

    DESCRIPTION$9

    _N_ N8

    SELLDATE

    N8

    SELLPRICE

    N8

    LISTDATE

    N8

    ISLASTN8

    D D

    List SellDescription Date Date

    Colonial 13803 14001Townhouse 13894 14016Townhouse 14108 14392

    Ranch 14585 14736Victorian 14805 15106

    Partial Listing of prog2.maysales

    data _null_;set prog2.maysales end=IsLast;file ' raw-data-file ';if _N_=1 then

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

    put DescriptionListDate : date9.SellDate : date9.

    SellPrice : dollar8.;if IsLast=1 then put 'Data: PROG2.MAYSALES';

    run;

    Execute

  • 8/14/2019 SAS Export

    20/35

    PDV

    ...

    DESCRIPTION$9

    _N_ N8

    1

    SELLDATE

    N8

    . .

    SELLPRICE

    N8

    LISTDATE

    N8

    .

    ISLASTN8

    0

    D D

    List SellDescription Date Date

    Colonial 13803 14001Townhouse 13894 14016Townhouse 14108 14392

    Ranch 14585 14736Victorian 14805 15106

    Partial Listing of prog2.maysales

    data _null_;set prog2.maysales end=IsLast;file ' raw-data-file ';if _N_=1 then

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

    put DescriptionListDate : date9.SellDate : date9.

    SellPrice : dollar8.;if IsLast=1 then put 'Data: PROG2.MAYSALES';

    run;

  • 8/14/2019 SAS Export

    21/35

    ......

    PDV

    DESCRIPTION$9

    Colonial

    _N_ N8

    1

    SELLDATE

    N8

    14001 355182

    SELLPRICE

    N8

    LISTDATE

    N8

    13803

    ISLASTN8

    0

    D D

    List SellDescription Date Date

    Colonial 13803 14001Townhouse 13894 14016Townhouse 14108 14392

    Ranch 14585 14736Victorian 14805 15106

    Partial Listing of prog2.maysales

    data _null_;set prog2.maysales end=IsLast;file ' raw-data-file ';if _N_=1 then

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

    put DescriptionListDate : date9.SellDate : date9.

    SellPrice : dollar8.;if IsLast=1 then put 'Data: PROG2.MAYSALES';

    run;

  • 8/14/2019 SAS Export

    22/35

    ......

    PDV

    DESCRIPTION$9

    Colonial

    _N_ N8

    1

    SELLDATE

    N8

    14001 355182

    SELLPRICE

    N8

    LISTDATE

    N8

    13803

    ISLASTN8

    0

    D D

    List SellDescription Date Date

    Colonial 13803 14001Townhouse 13894 14016Townhouse 14108 14392

    Ranch 14585 14736Victorian 14805 15106

    Partial Listing of prog2.maysales

    data _null_;set prog2.maysales end=IsLast;file ' raw-data-file ';if _N_=1 then

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

    put DescriptionListDate : date9.SellDate : date9.

    SellPrice : dollar8.;if IsLast=1 then put 'Data: PROG2.MAYSALES';

    run;

  • 8/14/2019 SAS Export

    23/35

    ......

    PDV

    DESCRIPTION$9

    Colonial

    _N_ N8

    1

    SELLDATE

    N8

    14001 355182

    SELLPRICE

    N8

    LISTDATE

    N8

    13803

    ISLASTN8

    0

    D D

    List SellDescription Date Date

    Colonial 13803 14001Townhouse 13894 14016Townhouse 14108 14392

    Ranch 14585 14736Victorian 14805 15106

    Partial Listing of prog2.maysales

    data _null_;set prog2.maysales end=IsLast;file ' raw-data-file ';if _N_=1 then

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

    put DescriptionListDate : date9.SellDate : date9.

    SellPrice : dollar8.;if IsLast=1 then put 'Data: PROG2.MAYSALES';

    run;Continue executing DATAstep. _N_ is equal to 1.

    Write header record

    to raw-data-file .

  • 8/14/2019 SAS Export

    24/35

    ......

    PDV

    DESCRIPTION$9

    Colonial

    _N_ N8

    1

    SELLDATE

    N8

    14001 355182

    SELLPRICE

    N8

    LISTDATE

    N8

    13803

    ISLASTN8

    0

    D D

    List SellDescription Date Date

    Colonial 13803 14001Townhouse 13894 14016Townhouse 14108 14392

    Ranch 14585 14736Victorian 14805 15106

    Partial Listing of prog2.maysales

    data _null_;set prog2.maysales end=IsLast;file ' raw-data-file ';if _N_=1 then

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

    put DescriptionListDate : date9.SellDate : date9.

    SellPrice : dollar8.;if IsLast=1 then put 'Data: PROG2.MAYSALES';

    run;IsLast is equal to 0.PUT statement is not

    executed.

  • 8/14/2019 SAS Export

    25/35

    ......

    DESCRIPTION$9

    Townhome

    _N_ N8

    2

    SELLDATE

    N8

    14108 241225

    SELLPRICE

    N8

    LISTDATE

    N8

    13894

    ISLASTN8

    0

    D D

    PDV

    List SellDescription Date Date

    Colonial 13803 14001Townhouse 13894 14016Townhouse 14108 14392

    Ranch 14585 14736Victorian 14805 15106

    Partial Listing of prog2.maysales

    data _null_;set prog2.maysales end=IsLast;file ' raw-data-file ';if _N_=1 then

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

    put DescriptionListDate : date9.SellDate : date9.

    SellPrice : dollar8.;if IsLast=1 then put 'Data: PROG2.MAYSALES';

    run;IsLast is equal to 0.PUT statement is not

    executed.

  • 8/14/2019 SAS Export

    26/35

    ......

    PDV

    DESCRIPTION$9

    Victorian

    _N_ N8

    5

    SELLDATE

    N8

    15106 358156

    SELLPRICE

    N8

    LISTDATE

    N8

    14805

    ISLASTN8

    1

    D D

    List SellDescription Date Date

    Colonial 13803 14001Townhouse 13894 14016Townhouse 14108 14392Ranch 14585 14736Victorian 14805 15106

    Partial Listing of prog2.maysales

    data _null_;set prog2.maysales end=IsLast;file ' raw-data-file ';if _N_=1 then

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

    put DescriptionListDate : date9.SellDate : date9.SellPrice : dollar8.;

    if IsLast=1 then put 'Data: PROG2.MAYSALES';

    run;IsLast is equal to 1.PUT statement is

    executed. Write footer

    record to raw-data-file .

  • 8/14/2019 SAS Export

    27/35

  • 8/14/2019 SAS Export

    28/35

    Specifying an AlternateDelimiterSpecifying an AlternateDelimiter

    Use the DLM= option in the FILEstatement to create a file with analternate delimiter (other than a blank).

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

    FILE file-specification DLM='quoted-string ';

    FILE file-specification DLM='quoted-string ';

  • 8/14/2019 SAS Export

    29/35

    Writing to an External FileWriting 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'

  • 8/14/2019 SAS Export

    30/35

    Writing to an External FileWriting to an External File

    What is the role of each comma inthe records containing data?

    proc fslist fileref=' raw-data-file ';run;

    Description,ListDate,SellDate,SellPriceColonial,16OCT1997,02MAY1998,$355,183Townhouse,15JAN1998,17MAY1998,$241,225Townhouse,17AUG1998,28MAY1999,$238,136Ranch,07DEC1999,06MAY2000,$219,392Victorian,14JUL2000,11MAY2001,$358,187

    Data: PROG2.MAYSALES

  • 8/14/2019 SAS Export

    31/35

    ......

    Partial Output

    Embedded DelimitersEmbedded Delimiters

    These commas act as delimiters. The DLM= option in theFILE statement causes their appearance.

    This comma acts as part of a SAS format. The DOLLAR8.format in the PUT statement causes its appearance.

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

    How many data values are contained in each record?How many comma-delimited fields are in each record?

  • 8/14/2019 SAS Export

    32/35

    Accounting for EmbeddedDelimitersAccounting for EmbeddedDelimiters

    Use the DSD option in the FILE statement towrite data items that contain embeddeddelimiters.

    Any data item that contains the specifieddelimiter is quoted with the double quotationmark (") when the data item is output.

    FILE file-specification DLM='quoted-string ' DSD;

    FILE file-specification DLM='quoted-string ' DSD;

  • 8/14/2019 SAS Export

    33/35

    Writing to an External FileWriting to an External File

    data _null_;

    set prog2.maysales;

    file ' raw-data-file ' dlm=',' dsd;put Description

    ListDate : date9.SellDate : date9.SellPrice : dollar8.;

    run;

  • 8/14/2019 SAS Export

    34/35

    Writing to an External FileWriting 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"

  • 8/14/2019 SAS Export

    35/35

    Questions