DATA Step, Macro, Functions and more

How to correct a text file on sas

Reply
Occasional Contributor
Posts: 5

How to correct a text file on sas

Hi, 

 

I have to import the folliwing text file : 

 

1 x1 2 x2

10 11

20 21

30 31

 

and must read it like this :

 

x1 x2

10 11

20 21

30 31

...

I am a complete beginner and couldn't find how to do it through the tutorials etc. 

Thank you.

Super User
Posts: 17,784

Re: How to correct a text file on sas

Did you try using the point and click interface? Which tool are you using?

Here's a link to examples on importing text files:
http://www.ats.ucla.edu/stat/sas/faq/read_delim.htm

Additionally, the SAS Programming 1 course is freely available.
Occasional Contributor
Posts: 5

Re: How to correct a text file on sas

Here is my program : 

 

Filename Fichier "/folders/myshortcuts/desktop/donnees-brutes.txt";

Data don; 

Infile Fichier Firstobs = 2; 

Input 1 x1 2 x2;

run;

 

But it tells me that there is a problem with my syntax because indeed the variables are messed up...

Do you have a solution so I can tell SAS to drop the "1" and "2"? 

SAS Super FREQ
Posts: 8,742

Re: How to correct a text file on sas

I don't quite understand what you mean that the program has to work as a "general solution". If the first "row" of your file contains the variable names to use and the data starts on row 2, then you will need to make 2 "reads" of the file. The first program will only read row 1 to determine how many variables/columns are on the rest of the rows and then the second program will use the information from the first row in an INPUT statement.

This will probably involve using a macro program, but the best suggestion I have is to write the whole set of programs first to make sure you really understand how to write the program before introducing anything to do with macro processing.

Also suggestion you read the rules for naming SAS variables. SAS variable names cannot start with a number, so the INPUT statement you show will error out because you cannot have a variable named "1" or "2".

cynthia
Occasional Contributor
Posts: 5

Re: How to correct a text file on sas

Yes it seems I have to use something called %INPUT,  which will read my data for a second time, but I do not understand how it works. 

Super User
Posts: 5,256

Re: How to correct a text file on sas

Is the first line the variable names?

1 is not a valid SAS name, and I guess that's why you want to remove it.

My suggestion is to just skip line 1 in your import, and define the variable names explicitly.

Tutorials/samples to look for - use key words "data step" "infile" input".

Data never sleeps
Occasional Contributor
Posts: 5

Re: How to correct a text file on sas

Yes these are the variables, and i must find a general solution, regardless of the number of variables.

 

Do you have any tricks? 

Super User
Posts: 5,256

Re: How to correct a text file on sas

Hm, do you mean that you always get files with an extra number in the first position of the file?

One (preferred) option is to have the files created for you with a different layout.

 

You could perhaps a script (SAS or DOS/shell) that removes that prior to import it to SAS?

By "general solution", does it mean you wish not to specify variable names in your import program, they always should be assigned automatically? When building robust system best practice would to explicitly define record layout. Automation may sound simple, but will often lead to ad-hoc post-import-programming.

Data never sleeps
Occasional Contributor
Posts: 5

Re: How to correct a text file on sas

Actually I am supposed to find something like this : 

 

Filename Fichier "/folders/myshortcuts/desktop/donnes-brutes.txt";
Data _null_ ;
set MAK;
Infile Fichier Firstobs = 2;
Input @@x1 @@x2;
If type= "NUM" then input @@x1;
else input @@x2;
run;

%include Fichier;
filename Fichier;

 

But that's not right.. 

Respected Advisor
Posts: 3,887

Re: How to correct a text file on sas

Is below doing what you're looking for or is there something else required with your real data?

Data don;
  /*Infile Fichier Firstobs = 2 truncover dlm=' ' dsd; */
  Infile datalines Firstobs = 2 truncover dlm=' ' dsd;
  Input x1 x2;
  datalines;
1 x1 2 x2
10 11
20 21
30 31
;
run;
Super User
Posts: 17,784

Re: How to correct a text file on sas

This is sounding a bit like homework now...regardless, it may help if you attach a sample of the text file.

Super User
Posts: 5,081

Re: How to correct a text file on sas

It sounds like you just need a small change to your INPUT statement, making it:

 

input x1 x2;

 

If you have the possibility of missing values in your data it will get more complex.  But let's see if that's actually a problem first.

Super User
Posts: 9,671

Re: How to correct a text file on sas

If there are not too many variables in the file, that would be easy, otherwise you need change that file .

 

 

data have;
infile '/folders/myfolders/temp.txt' firstobs=2 dlm=' ';
input x1 x2;
run;
Ask a Question
Discussion stats
  • 12 replies
  • 253 views
  • 0 likes
  • 7 in conversation