Read ASCII file

Reply
Contributor
Posts: 67

Read ASCII file

How to read ASCII file in SAS 9.3 ? since I want to convert it to the Excel file. beside that, I have dat file and inp file as well. Any help will be very appreciate. thanks.

Super User
Posts: 11,343

Re: Read ASCII file

Is the ASCII file delimited, fixed column or some other layout?

An example of what the file looks like could be very helpful in answering your question.

If the only thing you you are going to do is convert to Excel it may be worth going through the Excel import wizard.

Contributor
Posts: 67

Re: Read ASCII file

is not delimited. for example:

10000001201 601asprin                  V32.0

10100038 31 103MULTI-VITAMIN     V32.0

what I want to have is:

10000000 20 1 601 aspirin V32.0    ----  there are 'blank space' after the first '8 digits#', then '2 digits#', 'space','1 digit#', 'space', '3digits#', ,space', 'aspirin', 'space', 'v32.0'.

10100038  3 1 103 Multi-vitamin V32.0

sorry if I confuse you.

I try to import using Excel wizard, but there was any option for import .dat. file, so I chose .txt file, but the results is mess up, is not what I want.

thanks for your response.

Super User
Posts: 11,343

Re: Read ASCII file

It looks like have a fixed column file where each field starts in the same character position on the line. The approach to read with SAS would be to use an INFILE statement to tell which file to read then an INPUT statement that tells which columns to read for each variable.

Something like:

Data want;

     infile "c:\data.dat" ; /* may need options if line is longer than 256*/

     input var1 1-8 var2 9-10 var3 11 var4 12-15 var5 $ 16-39 var6 $ 40-44; /* using $ says to treat the variable as character not numeric*/

run;

Or start Excel, using menus go to File-Open, navigate to the folder the file is in, set the file types to ALL Files (*.*) not All Excel files as you want to read something that doesn't have an existing Excel format. You will get a wizard box that displays the first few lines of the data and some options. Click on the Fixed width. If the actual data NOT headers you want starts on a row other than the first put the number of the first data row in the little box. Click on Next. There will be a few rows of data displayed with a ruler. You click in the body of the data to indicate where the values split. In your example above between the 100000001 and 20, between the 20 and the 1. A vertical line will display your choice. If you make a mistake double click the line to remove it. After you have indicated all of the breaks between the data fields, click next. You now have the option to tell Excel if each column is numeric or text or date and some appearance options. Click in each column and set options or skip the column. Then finish. If happy save it. Else start over.

Contributor
Posts: 67

Re: Read ASCII file

thank you very much!  I will try it later and will let you know.

I have another question, how to delete the default title of the Means procedure on the report?---- The Means Procedure --- I wan to suppress this title.


Contributor
Posts: 67

Re: Read ASCII file

never mind, I have got it. Thanks!

instead, how to suppress the "obs" in the Mean report. ----  obs N   mean    min    max

                                                                                         1   50    0.5      0.15   2.5

Super User
Posts: 11,343

Re: Read ASCII file

Assuming you are using a CLASS statement then adding NONOBS to the PROC MEANS statement should suppress the number of observations, OBS.

Super User
Super User
Posts: 7,039

Re: Read ASCII file

It depends how you generated that report.  It looks a little like the output of PROC PRINT.  If so you can add the NOOBS option to the PROC PRINT statement.

proc print data=summary noobs;

  var n mean min max;

run;

Contributor
Posts: 67

Re: Read ASCII file

thank you very much, I have been successfully convert the .dat file into Excel file, it's awsome Smiley Happy.

Ask a Question
Discussion stats
  • 8 replies
  • 653 views
  • 1 like
  • 3 in conversation