UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

SAS Code Fragments
Changing tabs to spaces

Say you have a raw data file that uses both tabs and spaces as delimiters.  How do you read the raw data file?  In this example, we read the raw data file reading the entire observation as one long string (up to 1000 characters, but you can change that) and convert the tabs to spaces and then read the raw data file back in using spaces as delimeters.

data test;
  * read in original file as one big character variable ;
  * this assumes the longest line is less than 1000 chars ;
  * reads file from e:\temp\test.txt, change filename as needed ;
  infile "e:\temp\test.txt" missover pad lrecl=1000;
  length a $ 1000 ;
  input a 1-1000 ;

  * convert tabs into spaces ;
  b=translate(a," ","09"x);

  * writes raw data out to e:\temp\test2.txt, change filename as needed ;
  file "e:\temp\test2.txt" lrecl=1000;
  put b ;
run;

* check first 10 obs read ;
proc print data=test(obs=10);
run;

* Now, read from the raw data from prior step ;
data test2;
  infile "e:\temp\test2.txt" lrecl=1000 missover ;
  * put names of your variables here ;
  input x y ;
run;

* check first 10 obs read ;
proc print data=test2(obs=10);
run;

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California