BookmarkSubscribeRSS Feed
cmcmains
Calcite | Level 5

Hello,

 

I am trying to use INFILE to read in a file that uses spaces as its delimiter, but has some variables that are missing spaces between them. The numeric variables are all a max of 2 characters long, but some end up being 8 or more because of their missing spaces. Is there a way to tell SAS only to read 2 characters into a numeric variable, and then move on even if there is no space following?

 

Thank you!

5 REPLIES 5
Shmuel
Garnet | Level 18

Please post some sample of your input text file and the list of expected variables with their max length. Without that it is difficult to explain how to do what you want.

One of possibilities is to read the whole record as one variable, compress it to remove spaces and then read by position. It needs to take in account string variables (text) - can a variable contain a space/s in between?

cmcmains
Calcite | Level 5

So an example would be:

 

80123 10 20 30 40 50 60708090 10 20 30 40 50 60 70 80 90 10 20 30 40 50 60

 

So I would be using "INPUT code x1-x24" to get the first chunk as a code, then 24 variables each with 2 characters as their length. Does that explain it a bit better?

data_null__
Jade | Level 19

If the 24 fields always contain exactly 2 digits then you could do something like this.  Read the ID then compress blanks and use column input.

filename FT15F001 temp;
data test;
   infile FT15F001 col=col missover;
   input ID @;
   putlog col=;
   _infile_ = compress(substrn(_infile_,col),' ');
   putlog _infile_;
   input @1 (x1-x24) (2.);
   parmcards;
80123 10 20 30 40 50 60708090 10 20 30 40 50 60 70 80 90 10 20 30 40 50 60
8012355 10 20 30 40 50 60708090 10 20 30 405060 70 809010 20 30 40 50 60
;;;;
   run;
proc print;
   run;

Capture.PNG

Shmuel
Garnet | Level 18

I understand that you have 24 numeric variables each is two digits.

The minimum length of a record is 24x2=48. I assume max length is about 72.

Check next code:

data want;
    infile "<path and file name>.txt" truncover;
    input code 5. a_line $char72.;
    a_line = compress(a_line,' '||'09'x); /* remove spaces and tabs  */
    array varx {*} x1-x24;
    do i=1 to length(a_line) by 2;
       varx(i) = input(substr(a_line,i,2),2.);
    end; drop i;
run;  
       
ballardw
Super User

@cmcmains wrote:

Hello,

 

I am trying to use INFILE to read in a file that uses spaces as its delimiter, but has some variables that are missing spaces between them. The numeric variables are all a max of 2 characters long, but some end up being 8 or more because of their missing spaces. Is there a way to tell SAS only to read 2 characters into a numeric variable, and then move on even if there is no space following?

 

Thank you!


That mean you do not have a space delimited file. A delimited file by definition will have a delimiter between every variable.

If the number of spaces varies between longer and shorter values then you almost certainly have either a TAB delimited file or fixed column data.

 

And paste example data text into a TEXT box opened on the forum with the </> icon. The message window will reformat white space, i.e. blanks, and what we see is very likely not what you have.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 882 views
  • 2 likes
  • 4 in conversation