BookmarkSubscribeRSS Feed
MakKK
Calcite | Level 5

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.

12 REPLIES 12
Reeza
Super User
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.
MakKK
Calcite | Level 5

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"? 

Cynthia_sas
SAS Super FREQ
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
MakKK
Calcite | Level 5

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. 

LinusH
Tourmaline | Level 20

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
MakKK
Calcite | Level 5

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

 

Do you have any tricks? 

LinusH
Tourmaline | Level 20

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
MakKK
Calcite | Level 5

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.. 

Patrick
Opal | Level 21

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;
Reeza
Super User

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

Astounding
PROC Star

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.

Ksharp
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 12 replies
  • 1191 views
  • 0 likes
  • 7 in conversation