DATA Step, Macro, Functions and more

Replace tabs with equal number of spaces

Reply
Contributor
Posts: 65

Replace tabs with equal number of spaces

[ Edited ]

I want to replace tabs with equal number of spaces in a string which I am reading from a file line by line. The no. of spaces may vary depending on the editor.How to determine the no of spaces and replace as per in SAS 9.3?

I have used expandtabs command while reading the file so that the tabs gets converted to spaces, but it didn't work as it expands a tab to 8 spaces(in the file tabs are having variable spaces).

Please help.

Super User
Super User
Posts: 6,497

Re: Replace tabs with equal number of spaces

You can use the EXPANDTABS option on the INFILE statement to expand tabs to normal 8 character tab stops. if you want to use other tab stops than evey 8 you will have to create you own algorithm.

 

data _null_;
   infile 'withtabs.txt' expandtabs;
   file 'withspaces.txt' ;
   input;
   put _infile_;
run;
Contributor
Posts: 65

Re: Replace tabs with equal number of spaces

[ Edited ]

Tried that..but it expands the tabs to 8 spaces. I want exact no of spaces which the tab originally have as I have to maintain the relative alignment of text in file.

Super User
Super User
Posts: 7,392

Re: Replace tabs with equal number of spaces

And how is your program going to know how many spaces a tab represented in another application?  That is the problem you see.  I would suggest that you need to go back to the source, and indicate that they should use spaces rather than tabs (which is good programming practice by the way, for this very reason).  

 

You could also read the file in as character stream, and write it out to a new file, replacing the tab character with a set of spaces which you provide:

data _null_;
  infile "C:\temp\test.csv" recfm=n;
  file "C:\temp\NEW_Test.csv" recfm=n;
  input a $char1.;
  if a='0A'x then put ' ';
  else put a $char1.;
run;

 

However you still need to know how many spaces, and its your responsibility if it goes wrong.  My opinion, get the person who used tabs in the first place to fix it.

 

Contributor
Posts: 65

Re: Replace tabs with equal number of spaces

I am able to determine the no of spaces on the fly in C#:

Public Function replaceTabWithSpace(ByVal lineToReplace As String) As String

Dim textWithSpaces = String.Empty

Dim textValues = lineToReplace.Split(ControlChars.Tab)

For Each val As String In textValues
textWithSpaces += Convert.ToString(val) & New String(" "c, 4 - val.Length Mod 4)
Next

Return textWithSpaces
End Function

However, since I am new to SAS programming, I don't know how is it done in Base SAS.
Also, since the files are existing files, asking the coder to take care of tabs is not an option.
Super User
Super User
Posts: 7,392

Re: Replace tabs with equal number of spaces

Yes, as mentioned tab is merely 1 character for which each editor of application will interpret to mean a different spacing.  You cannot know this in your programming, in any language.  All you are getting in C# is Visual Studio's idea of how much space should be there for a tab.  The problem lies with the source, what did they write the code in, once you know that then you can use my program to replace tabs with spaces.

Super User
Super User
Posts: 6,497

Re: Replace tabs with equal number of spaces

Here is one way.

 

data xx;
  length source target $200 ;
  tabstop=4 ;
  source = catx('09'x,'1','56','9');
  pos=0;
  do i=1 to length(source);
    char = substr(source,i,1);
    if char='09'x then do;
      pos=pos + (tabstop - mod(pos,tabstop)) ;
    end;
    else do ;
      pos=pos+1;
      substr(target,pos,1)=char ;
    end;
  end;
run;
Super User
Posts: 6,928

Re: Replace tabs with equal number of spaces

"The no. of spaces may vary depending on the editor"

That is exactly the problem. You cannot know the original span just from the file that contains the tabs, you need to know that value beforehand.

 

So you either get that value from the person supplying the file, or they save with tabs expanded to spaces. Simple as that.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 212

Re: Replace tabs with equal number of spaces

[ Edited ]

Please if possible attach one example of file that you might have.

Or just paste here a bit of it. Put the raw format the file as it is and the desired format.

 

I think i had the same trouble.

Super User
Posts: 10,466

Re: Replace tabs with equal number of spaces

It might help to expand on what you mean by "reading from a file line by line". Generally we are reading a file for data and TAB is a common delimiter between fields. The length does not matter at all in that case.

 

In base SAS for the enhanced editor there are preferences to tell the editor how many spaces to indent for a tab. Tools>Options>Enhanced Editor. You specify the number of characters and you can check the "Insert Spaces for tabs" and "Replace tabs with spaces on file open".

With those settings if you open the file in the Enhanced editor and save then you should have the tabs replaced with your desired number of spaces. The key will be to set the number in the options BEFORE opening the file.

Ask a Question
Discussion stats
  • 9 replies
  • 875 views
  • 1 like
  • 6 in conversation