DATA Step, Macro, Functions and more

Raw txt File

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

Raw txt File

Hi mates,

 

I need to read a raw datafile just like this:

 

Joe Smith

20 M 35000 BS

Jane Doe

24 F 50000 MS

Matt Brown

30 M 65500 HS

Mike Johnson

55 M 85000 PhD

 

I've tryed the following code but doesn't work:

 

data new;
 infile "path/test.txt";
 length fname $6 lname $10 sex $1 education $3;
 informat lname $10. income 7.;
 input fname 1-6 @7 lname /
 age 1-3 sex +1 income @14 education;
run;

The code is from this link: http://www.lexjansen.com/nesug/nesug08/ff/ff01.pdf

 

I need a way to tell the input statement when it needs to read the next line, how can i do that ? I forgot.

 

Thanks

 


Accepted Solutions
Solution
‎10-30-2015 12:41 PM
Respected Advisor
Posts: 4,649

Re: Raw txt File

Use list input:

 

data new;
 length fname $6 lname $10 sex $1 education $3;
 input fname lname /
 age sex income education;
datalines;
Joe Smith
20 M 35000 BS
Jane Doe
24 F 50000 MS
Matt Brown
30 M 65500 HS
Mike Johnson
55 M 85000 PhD
;

although it might be safer to read the whole name since it might have more than two words:

 

data new2;
 length name $24 sex $1 education $3;
 input name &/
 age sex income education;
datalines;
Joe Smith
20 M 35000 BS
Jane Doe
24 F 50000 MS
Matt Brown
30 M 65500 HS
Mike Johnson
55 M 85000 PhD
;
PG

View solution in original post


All Replies
Super User
Posts: 17,829

Re: Raw txt File

[ Edited ]
Solution
‎10-30-2015 12:41 PM
Respected Advisor
Posts: 4,649

Re: Raw txt File

Use list input:

 

data new;
 length fname $6 lname $10 sex $1 education $3;
 input fname lname /
 age sex income education;
datalines;
Joe Smith
20 M 35000 BS
Jane Doe
24 F 50000 MS
Matt Brown
30 M 65500 HS
Mike Johnson
55 M 85000 PhD
;

although it might be safer to read the whole name since it might have more than two words:

 

data new2;
 length name $24 sex $1 education $3;
 input name &/
 age sex income education;
datalines;
Joe Smith
20 M 35000 BS
Jane Doe
24 F 50000 MS
Matt Brown
30 M 65500 HS
Mike Johnson
55 M 85000 PhD
;
PG
Regular Contributor
Posts: 212

Re: Raw txt File

PG,

 

What i need actually is to create a three positional input due the following txt file:

 

 

F   015   3  20151029 019  0100             00000000000000 20151029 102004 20151029                     47,28     1530215000019
591 PAGAMENTO INCLU
019  0100             00000000000000 20151029 102155 20151029                    212,50     1530215000020
.   .
018  0023             00000000000000 20151029 103258 20151029                    538,85     1530215000021

 

The first line will start with the F in the column1, 015 in the column 2 and 3 in the column3, all the other values should match for the first row, but in the third row the "019" should be at the same position of "019" in the first line.

 

To understant what i'm saying, the first textbox is how my raw file is in SAS, the second textbox bellow is represented on how the third row should begin:

 

 

F   015   3  20151029 019  0100             00000000000000 20151029 102004 20151029                     47,28     1530215000019
591 PAGAMENTO INCLU
                      019  0100             00000000000000 20151029 102155 20151029                    212,50     1530215000020
.   .
                      018  0023             00000000000000 20151029 103258 20151029                    538,85     1530215000021

So what i want is to read the first and the second line and print the into the first line.

 

The third row should begin to read or print the value at the same column of the "019" and put missing to the first 4 columns.

 

The final output i'm trying to make is this:

 

 

F| 015| 3| 20151029   |   019 | 0100 | 00000000000000| 20151029 |102004| 20151029|   47,28     |1530215000019| 591| PAYMENT
 |    |  |            |   019 | 0100 | 00000000000000| 20151029 |102155| 20151029|  212,50     |1530215000020| 591| PAYMENT
 |    |  |            |   018 | 0023 | 00000012344534| 20151029 |103258| 20151029|  538,85     |1530215000021|    |

 

But i'm having a lot trouble to make this.

Can you help me ?

 

Thanks in advance

 

Super User
Posts: 10,500

Re: Raw txt File

Expect problems when you force reading fixed columns and your data may not:

input fname 1-6 @7 lname /

will always read the first six columns for the first name but

123456

Joe Smith

so first name likely ends up as "Joe Sm" and the last name starts with "ith" as the "i" is in the 7th column.

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 325 views
  • 3 likes
  • 4 in conversation