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.
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"?
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.
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".
Yes these are the variables, and i must find a general solution, regardless of the number of variables.
Do you have any tricks?
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.
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..
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;
This is sounding a bit like homework now...regardless, it may help if you attach a sample of the text file.
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.