Help using Base SAS procedures

how to import data to SAS without some literals from text file?

Reply
Occasional Contributor
Posts: 9

how to import data to SAS without some literals from text file?

I have a text file test.txt like this :

This is class room 1

ID STUDENTNAME AGE WEIGHT(pounds)

1 Robert 13 70
2 Mary 12 75
3 Nicole 11 79

This is class room 2

ID STUDENTNAME AGE WEIGHT(pounds)

4 charles 15 80
5 Kathy 14 85
6 Jen 10 70

The above is test.txt file.I have to import it as data set given below:

ID STUDENTNAME AGE WEIGHT(pounds)

1 Robert 13 70
2 Mary 12 75
3 Nicole 11 79
4 charles 15 80
5 Kathy 14 85
6 Jen 10 70
PROC Star
Posts: 7,471

Re: how to import data to SAS without some literals from text file?

There a probably a number of ways. One might be something like:
[pre]
data need;
infile "c:\test.txt";
length id $3;
length studentname $10;
input;
if _infile_ ne "" then do;
if anydigit(substr(_infile_,1,1)) then do;
id=scan(_infile_,1);
studentname=scan(_infile_,2);
age=input(scan(_infile_,3),best12.);
weight_pounds=input(scan(_infile_,4),best12.);
output;
end;
end;
run;
[/pre]
Art
> I have a text file test.txt like this :
>
> This is class room 1
>
> ID STUDENTNAME AGE WEIGHT(pounds)
> 1 Robert 13 70
> 2 Mary 12 75
> 3 Nicole 11 79
> This is class room 2
>
> ID STUDENTNAME AGE WEIGHT(pounds)
> 4 charles 15 80
> 5 Kathy 14 85
> 6 Jen 10 70
> e above is test.txt file.I have to import it as data
> set given below:
>
> ID STUDENTNAME AGE WEIGHT(pounds)
> 1 Robert 13 70
> 2 Mary 12 75
> 3 Nicole 11 79
> 4 charles 15 80
> 5 Kathy 14 85
> 6 Jen 10 70
Super User
Posts: 10,028

Re: how to import data to SAS without some literals from text file?

[pre]
data need(drop=row);
infile "c:\temp\test.txt" length=len;
input row $varying200. len;
if not missing(input(scan(row,1),?? best10.)) then do;
id=scan(row,1);studentname=scan(row,2);age=scan(row,3);weight=scan(row,4);
output;
end;
run;
[/pre]


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