BookmarkSubscribeRSS Feed
Aditi24
Obsidian | Level 7

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.

9 REPLIES 9
Tom
Super User Tom
Super User

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;
Aditi24
Obsidian | Level 7

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

Aditi24
Obsidian | Level 7
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.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Tom
Super User Tom
Super User

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

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

DartRodrigo
Lapis Lazuli | Level 10

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.

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 6115 views
  • 1 like
  • 6 in conversation