Advertisements

9 Practical Examples to Import Text Files into SAS (Example Codes!)

In this section, we discuss 9 practical examples to import Text Files into SAS. SAS users often use data from different sources, and the data sets are of different types. Some of them are in Excel Files, Text Files, STATA Files, Python, and many others. An expert user must know how to import these different types of files into SAS. In this section, we show, with example codes, 6 easy ways to import Text Files into SAS. We will use both PROC IMPORT and DATA STEP. This post is an extension to Tricks for importing text files into SAS, and it has more example codes. You can easily copy the codes.

9 practical examples to import text files into SAS. Import Text File into SAS. How to import TXT file into SAS. Import TXT in SAS

1. Import a Generic Text File into SAS with PROC IMPORT

To import Text File into SAS, the generic PROC IMPORT statement is:

/*Generic Formula to import Text File into SAS*/

proc import file="Location\grade1.txt"
    out=work.File_Name
    dbms=tab
    replace;    //Use "REPLACE" if you have already have the data set in SAS//
run;

Example: We have a data set “grade1.txt” located in the folder C:\wikitekkee\Learning SAS. We want to import the file into SAS. To do so, the code we use:

* To import text file into SAS*/

proc import file="C:\wikitekkee\Learning SAS\grade1.txt"
    out=work.grade1
    dbms=tab
    replace;
run;

/* To print the data and check whether the data set has been properly imported into SAS */

proc print data=grade1 (obs=5) noobs;
run;

Example data set: From Text to SAS File:

Import Text File into SAS. Proc Import Text File into SAS. How to open Text file in SAS.

2. How to import Tab Delimited Text File into SAS:

To import and replace Tab Delimited Text File into SAS, we will use the PROC IMPORT. The Generic Statement is as follows:

/* To import Text File with Tab Delimted */

proc import file="File Location\File_Name.csv"
    out=work.FileName
    dbms=tab
    replace;  /* Use REPLACE if you already have the data in SAS and want to replace with the new one *?
run;

In our example, we are using the following Tab Delimited data of AMAZON.

Example: We have the Tab Delimited data set amazon_tab.csv, which we want to import into SAS by using PROC IMPORT statement. You can download the data set by clicking on the Download Data.

Tab Delimited Data

The amazon raw data is as follows:

Example Code: The PROC IMPORT statement is as follows:

Advertisements
/* Import Text File with Tab Delimited */

proc import file="C:\wikitekkee\Learning SAS\amazon_tab.csv"
    out=work.amazontab
    dbms=tab
    replace;
run;

/* To print and check whether the data set has been properly imported */

proc print data=amazontab;
run;

The Example files: from Tab Delimited to SAS

3. How to import Comma Delimited Text File into SAS

To import comma (“,”) delimited Text File into SAS, the generic PROC IMPORT statement is:

/* Generic PROC IMPORT statement to import Comma Delimited Text File into SAS */

proc import file="File Location\File_Name.txt"
    out=work.File_Name
    dbms=tab
    replace;      /* Use REPLACE if you already have the data set in SAS and want to replace */
    delimiter=",";
run;

Example Data set: We use the amazon_comma.txt data set, and you can download the data set by clicking on the Download Data below.

Comma Delimited Data

The Comma Delimited data set:

Example Code: The Example PROC IMPORT statement to import Comma Delimited data set into SAS is as follows:

/* Import Comma Delimited Text File into SAS */

proc import file="C:\wikitekkee\Learning SAS\amazon_comma.txt"
    out=work.amazoncomma
    dbms=tab
    replace;
    delimiter=",";
run;

/* To print and check whether the data has been properly imported into SAS */

proc print data=amazoncomma;
run;

Example File: Form Comma Delimited to SAS:

Import Comma delimited Text File into SAS.

4. How to import Semicolon Delimited Text File into SAS

To import semicolon (“;”) delimited Text File into SAS, the generic PROC IMPORT statement is:

/* Generic PROC IMPORT Statement to import Semicolon (";") Delimited Text File into SAS */

proc import file="File Location\amazon_space.txt"
    out=work.FIleName
    dbms=tab
    replace;    /* Use REPLACE if you aready have the data set in SAS and want to replace */
    delimiter=";";
run;

Example Data set: We use the amazon_semi.txt data set, and you can download the data set by clicking on the Download Data below.

Semicolon Delimited Data

Download Data

Advertisements

Semicolon Delimited Data set is as follows:

Import Semicolon (";") delimited data into SAS

Example Code: To import Semicolon (“;”) delimited Text File into SAS, the Example PROC IMPORT statement is as follows:

/* Import Semicolon (";") Delimited Text File into SAS */

proc import file="C:\wikitekkee\Learning SAS\amazon_semi.txt"
    out=work.amazonsemi
    dbms=tab
    replace;
    delimiter=";";
run;

/* To print and check whether the data has been properly imported into SAS */

proc print data=amazonsemi;
run;

Proc Print shows the following table:

Import Semicolon Delimited Text file into SAS

5. How to import Space Delimited Text File into SAS

To import space (” “) delimited Text File into SAS, the generic PROC IMPORT statement is:

/* Generic PROC IMPORT Statement to import Space " " Delimited Text File into SAS */

proc import file="File Location\amazon_space.txt"
    out=work.FIleName
    dbms=tab
    replace;    /* Use REPLACE if you aready have the data set in SAS and want to replace */
    delimiter=" ";
run;

Example Data set: We use the amazon_space.txt data set, and you can download the data set by clicking on the Download Data below.

Space Delimited Data

The Space Delimited Data set is as follows:

Import Comma Separated Text File into SAS

Example Code: The Example PROC IMPORT statement is as follows:

/* Import Space " " Delimited Text File into SAS */

proc import file="C:\wikitekkee\Learning SAS\amazon_space.txt"
    out=work.amazonspace
    dbms=tab
    replace;
    delimiter=" ";
run;

/* To print and check whether the data has been properly imported into SAS */

proc print data=amazonspace;
run;

6. How to import Pipe Delimited Text File into SAS

To import pipe (“|”) delimited Text File into SAS, the generic PROC IMPORT statement is:

/* To import Pipe Delimited Text File into SAS, Generic PROC IMPORT statement */

proc import file="File Location\File_Name.txt"
    out=work.FileName
    dbms=tab
    replace;         / * Use REPLACE if you alredy have the data and want to replace */
    delimiter="|";  
run;

Example Data set: We use the amazon_pipe.txt data set, and you can download the data set by clicking on the Download Data below.

Pipe Delimited Data

Advertisements

The raw data is as follows:

Import Pipe Delimited Text File into SAS.

Example Code: The Example PROC IMPORT statement is as follows:

/* To import Pipe Delimited Text File into SAS */

proc import file="C:\wikitekkee\Learning SAS\amazon_pipe.txt"
    out=work.amazonpipe
    dbms=tab
    replace;
    delimiter="|";
run;

/* To print and check whether the data set has been properly imported into SAS */

proc print data=amazonpipe;
run;

The results from the proc print shows the following table:

Import pipe delimited Text File into SAS.

7. How to import Hyphen (“-“) delimited Text File into SAS

To import hyphen (“-“) delimited Text File into SAS, the generic PROC IMPORT statement is:

/* Import hyphen ("-") Delimited Text File into SAS */

proc import file="File Location\FileName.txt"
    out=work.FileName
    dbms=tab
    replace;
    delimiter="-";
run;

Example Data set: We use the amazon_hyphen.txt data set, and you can download the data set by clicking on the Download Data below.

Hyphen Delimited Data

The hyphen (“-“) delimited data is as follows:

Import hyphen delimited Text File into SAS.

Example Code to import Hyphen (“-“) Delimited Text File into SAS: The Example PROC IMPORT statement is as follows:

/* Import hyphen ("-") Delimited Text File into SAS */

proc import file="C:\wikitekkee\Learning SAS\amazon_hyphen.txt"
    out=work.amazonhyp
    dbms=tab
    replace;
    delimiter="-";
run;

/* To print and check whether the data has been properly imported into SAS */

proc print data=amazonhyp;
run;

Output from the proc print statement:

Import Hyphen delimited Text File into SAS

8. How to import Text file into SAS using PROC IMPORT and getnames

We can also use the PROC IMPORT with getnames. The example statement is as follows:

Advertisements
/* To import TXT files into SAS using PROC IMPORT with getnames */

proc import datafile="C:\wikitekkee\Learning SAS\amazon.txt" out=amazon dbms=dlm replace;
 delimiter=',';
   getnames=yes;
run;

/* To print and check whether the data set has been properly imported */

proc print data=amazon;
run;

9. How to import STATA File into SAS

To import STATA Files into SAS, the example statement is as follows:

/* To import STATA file into SAS */

proc import file="C:\wikitekkee\Learning SAS\grade2.dta"
out=work.grade2
dbms=DTA
replace;
run;

/* To print the data and check whether the data set has been properly imported into SAS */

proc print data=grade2;
run;

For the full post on how to import STATA files into SAS, read the post.

10. More related readings

  1. How to import Excel Files into SAS
  2. How to import STATA Files into SAS

4 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Advertisements