SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

read data from txt file

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

read data from txt file

I have some irregular data in .dat file such as like below.

I hope to search for a SAS code that can read the data sequentially, starting from 25, 6, then 6, then 1277, then 1.7, 31.4, 78.1, ....., etc.

Is there any easy way to do that?

 

 

 

 

25 6
6
      1277
  1.7  31.4  78.1   2.4

  0.2   0.2   0.4   0.4   0.4   0.2   0.3   0.4   0.3   0.3   0.3   0.3   0.3   0.3   0.2   0.3   0.3   0.2   0.3   0.3   0.2   0.4   0.3   0.2   0.2
   47   159    48     1    45   177    83   121    50    31   164   121   135   157    54   167    64  

  21    43   149    30   165   173    96   167


Accepted Solutions
Solution
‎01-08-2016 01:22 PM
Super User
Posts: 17,829

Re: read data from txt file

In the future, it helps if you attach the actual data file rather than word documents. When you attach a word document you're relying on me creating the file and then writing the code for you as well.  

 

This code works, you may need to tweak the LRECL. Basically read in the entire line as a text field and then parse it out using the SCAN() function.

 

data want;
infile 'C:\_localdata\delete.txt' lrecl=5000;
input;

x=countw(_infile_);

do i=1 to x;
Num=scan(_infile_, i, " ");
output;
end;

drop i x; run;

 

 

View solution in original post


All Replies
Super User
Posts: 17,829

Re: read data from txt file

Can you attach a sample of your text file and post expected output?
Contributor
Posts: 52

Re: read data from txt file

I have attached file with input data and expected output data.  I have thousands of such files that have different length of such data, so it would be helpful if the code is not strict to a specific number of data.  Appreciate your help.

Solution
‎01-08-2016 01:22 PM
Super User
Posts: 17,829

Re: read data from txt file

In the future, it helps if you attach the actual data file rather than word documents. When you attach a word document you're relying on me creating the file and then writing the code for you as well.  

 

This code works, you may need to tweak the LRECL. Basically read in the entire line as a text field and then parse it out using the SCAN() function.

 

data want;
infile 'C:\_localdata\delete.txt' lrecl=5000;
input;

x=countw(_infile_);

do i=1 to x;
Num=scan(_infile_, i, " ");
output;
end;

drop i x; run;

 

 

Contributor
Posts: 52

Re: read data from txt file

The post does not accept .dat format.  Sorry for the trouble.  Your help is greatly appricated.

 

Tao

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 485 views
  • 0 likes
  • 2 in conversation