09-18-2015 06:04 AM - edited 09-18-2015 06:16 AM
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).
09-18-2015 06:12 AM
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;
09-18-2015 06:18 AM - edited 09-18-2015 06:19 AM
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.
09-18-2015 06:27 AM
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:
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.;
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.
09-18-2015 06:40 AM
09-18-2015 06:54 AM
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.
09-18-2015 06:57 AM
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;
09-18-2015 06:41 AM
"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.
09-18-2015 09:53 AM - edited 09-18-2015 09:57 AM
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.
09-18-2015 11:35 AM
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.