BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DartRodrigo
Lapis Lazuli | Level 10

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
PGStats
Opal | Level 21

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
DartRodrigo
Lapis Lazuli | Level 10

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

 

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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