Need help regarding ASCII format

Reply
Occasional Contributor M_K
Occasional Contributor
Posts: 14

Need help regarding ASCII format

hi,

I had a couple of questions around ASCII file:

  • Suppose i have 1000s of variables in an ASCII file and I do not want to define all of them separately. Can we define the variables in block in SAS, say blocks of 500 variables?
  • While exporting the data file to ASCII format, it gives first row as variable names and also give "tab"s between the values? Is there any way to export the data from SAS to proper ASCII with correct column positions as the original file format (without variable names and without "tab"s) and also with a data map? I am currently using following code:

    

PROC EXPORT

DATA=libname.data

OUTFILE="D:\xyz\test.asc"

DBMS=tab REPLACE;

run;

Any help will be appreciated! Thanks!

Super User
Posts: 11,343

Re: Need help regarding ASCII format

I don't understand what you mean by blocks of variables but you can on an input statement use things like:

input Var1-var500 Varb1-Barb500;

Which will read the data into Var1 var2 var3 var4 ... var500 Varb1 Varb2 Varb3 .... Varb500

PROC EXPORT has minimal control over output, one it doesn't is variable order on export. You are getting tabs because you requested them in the DBMS=tab statement.

The old school to give you complete control over output to ascii is to use a data _null_, file and put statements. If you want to create fixed column output then there are ways using column pointers to specify either absolute or relative spacing between variables, which formats to use to control appearance of values in the output.

Data _null_;

     file "C:\my text file.txt" lrecl=<a value at least as large as the number of columns you expect>;

     set datasetname;

     put var1-var500 ;

run;

Occasional Contributor
Posts: 7

Re: Need help regarding ASCII format

In addition to the response above, you can provide a little more detail. For example, when reading data into SAS from ASCII, you can use the LENGTH statement:

LENGTH V1-V500 $6 V501-V700 8 V701-V900 $1;

INPUT V1-V900;

You can control the delimiter for the input or output file by using the DLM option:

FILENAME TEXT "my documents\stuff\things.txt";

DATA WHATEVER;

INFILE TEXT DLM="|" DSD LRECL=9999 MISSOVER;

INPUT V1-V900;

RUN;

DLM tells SAS what to use as a separator; DSD tells SAS that consecutive separators imply a missing value. I always identify the input record length, because early on PC-SAS defaulted to (I seem to recall) 256 characters.

You can use the DLM option on your FILE statement as well. As ballardw notes, you need to specify some LRECL value if you expect a long file.

You can also create a fixed-format file by using variations on the PUT statement, e.g.,

DATA _NULL_;

SET WORK.STUFF;

FILE "MY DOCUMENTS\TEXT_STUFF.TXT" LRECL=999;

PUT @1 ID

       @10 ADDRESS

       @50 PHONE;

RUN;

Ask a Question
Discussion stats
  • 2 replies
  • 497 views
  • 0 likes
  • 3 in conversation