How to Write Raw Data in SAS – PROC Export, CSV file & Tab Separated File
In the last tutorial, we learned how to read Raw data in SAS, now, in this tutorial, we will be seeing how to write Raw data in SAS Programming Language.
So, let us begin with writing raw data in SAS Programming.
Different Ways to Write Raw Data in SAS
Here, we discuss three ways to write raw data in SAS Programming Language.
i. PROC Export Statement
Using proc export in SAS, you can easily write raw data in SAS to a file with values delimited by commas, tabs, spaces, or other characters. We will go through examples of how to write these out.
data cars; set sashelp.cars; run; proc contents data = cars; run;
It will look like this –
Alphabetic List of SAS Variables and Attributes
# Variable Type Len Format Label
9 Cylinders Num 8
5 DriveTrain Char 5
8 EngineSize Num 8 Engine Size (L)
10 Horsepower Num 8
7 Invoice Num 8 DOLLAR8.
15 Length Num 8 Length (IN)
11 MPG_City Num 8 MPG (City)
12 MPG_Highway Num 8 MPG (Highway)
6 MSRP Num 8 DOLLAR8.
1 Make Char 13
2 Model Char 40
4 Origin Char 6
3 Type Char 8
13 Weight Num 8 Weight (LBS)
14 Wheelbase Num 8 Wheelbase (IN)
The syntax of the PROC statement:
PROC EXPORT DATA=libref.SAS data-set (SAS data-set-options) OUTFILE="filename" DBMS=identifier LABEL(REPLACE);
Following is the description of the parameters used:
- SAS data-set is the data set name which exports. It uses the inbuilt EXPORT function to out the dataset files in a variety of formats.
- SAS data-set-options is to specify a subset of columns to export.
- Filename is the name of the file to which the data is written.
- Identifier is used to mention the delimiter that will be written into the file.
- LABEL option is used to mention the name of the variables written to the file.
Must Read – Datasets in SAS Programming
Let us look at the example below:
proc export data=sashelp.cars outfile='D:datacars' dbms=dlm; delimiter=' '; run;
ii. Writing a CSV file
If we wish to write raw data in SAS as a comma-separated file, then we can modify our outfile, specify CSV in the dbms option, and omit the delimiter line.
proc export data=sashelp.cars outfile='D:datacars.csv' dbms=csv; run;
Output- Make,Model,Type,Origin,DriveTrain,MSRP,Invoice,EngineSize,Cylinders,Horsepower,MPG_City,MPG_Highway,Weight,Wheelbase,Length Acura,MDX,SUV,Asia,All,”$36,945″,”$33,337″,3.5,6,265,17,23,4451,106,189Acura,RSX Type S 2dr,Sedan,Asia,Front,”$23,820″,”$21,761″,2,4,200,24,31,2778,101,172Acura,TSX 4dr,Sedan,Asia,Front,”$26,990″,”$24,647″,2.4,4,200,22,29,3230,105,183Acura,TL 4dr,Sedan,Asia,Front,”$33,195″,”$30,299″,3.2,6,270,20,28,3575,108,186Acura,3.5 RL 4dr,Sedan,Asia,Front,”$43,755″,”$39,014″,3.5,6,225,18,24,3880,115,197
Explore the SAS Operators Full Guide
iii. Writing a Tab Separated File
If we wish to write out our dataset as a tab-separated file, then we can modify our outfile, specify tab in the dbms option, and omit the delimiter line.
proc export data=sashelp.cars outfile='D:datacars.txt' dbms=tab; run;
Output- Make Model Type Origin DriveTrain MSRP Invoice EngineSize Cylinders Horsepower MPG_City MPG_Highway Weight Wheelbase LengthAcura MDX SUV Asia All $36,945 $33,337 3.5 6 265 17 23 4451 106 189Acura RSX Type S 2dr Sedan Asia Front $23,820 $21,761 2 4 200 24 31 2778 101 172Acura TSX 4dr Sedan Asia Front $26,990 $24,647 2.4 4 200 22 29 3230 105 183Acura TL 4dr Sedan Asia Front $33,195 $30,299 3.2 6 270 20 28 3575 108 186Acura 3.5 RL 4dr Sedan Asia Front $43,755 $39,014 3.5 6 225 18 24 3880 115 197
This was all on Write Raw data in SAS. Hope you like our explanation.
So, this section was all about how to write Raw data in SAS, different kinds of Raw datasets in SAS. Hope you all understood it well. Try this out using different datasets and you will find some wonderful results. Furthermore, if you have any query regarding How to Write Raw Data in SAS, feel free to ask in a comment section.