Help using Base SAS procedures

Reg Uncompressing Tab in the Data

Reply
Contributor
Posts: 66

Reg Uncompressing Tab in the Data

Actually i am having data coming in text file the delimiter in ^ actually in some obs there is tab but it was removing the tab also now i want to replace tab with space

below example data has tab between ra and if i run it was remoivng the tab and i wnat to replace it with spaces

data l2;
input id$ 1-7 age ;
cards;
ra  ok 56
run;
proc print;
run;

Super User
Super User
Posts: 7,039

Reg Uncompressing Tab in the Data

I am not clear what your question is.  But if you have a text file and you want to have SAS expand the tabs into space when reading it look at the EXPANDTABS option on the INFILE statement.

data l2;

   infile 'myfile.dat' expandtabs truncover ;

  input id$ 1-7 age ;

run;

Contributor
Posts: 66

Re: Reg Uncompressing Tab in the Data

data l2;
   infile cards expandtabs ;
  input id$ 1-57 ;
id1=compbl(id);
cards;
user   done    ok
;
run;


Actually i want to replace tabs with space in the above from user to done there is tab and from done to k there is space i want to replace the tab with the space actually i have tryed it compbl,trim functions

output

user done    ok

Super User
Super User
Posts: 7,039

Re: Reg Uncompressing Tab in the Data

You can use TRANWRD to change tabs into spaces.

id1=compbl(tranwrd(id,'09'x,' '));

If you really have tabs inside your datalines inside your program file then you might want to change the settings on your editor so that all tabs are converted to spaces when you save.   Personally I always do this as I find that tabs in source code cause more trouble than any benefit they might add.

Contributor
Posts: 66

Reg Uncompressing Tab in the Data

i have checked by tranwrd it was not working

Respected Advisor
Posts: 4,173

Reg Uncompressing Tab in the Data

It should work. See below:

data _null_;
  id='A'||'09'x||' A';
  put id hex.;
  id=compbl(tranwrd(id,'09'x,' '));
  put id hex.;
run;

Unless you work on z/OS as there the Hex value for a tab would be 05.

Super User
Super User
Posts: 7,039

Reg Uncompressing Tab in the Data

It must not be a TAB then.  Use either the list statement or the $HEX format to see what character it is.  Perhaps it is 'A0'x which some editors use to store a non-breaking space.

Respected Advisor
Posts: 4,173

Reg Uncompressing Tab in the Data

Below code should replace any whitespace character with a "blank":

data _null_;

  id='A'||'09'x||'0A'x||' A';

  put id hex.;

  id=compbl(prxchange('s/\s/ /o', -1, id));

  put id hex.;

run;

Super User
Super User
Posts: 7,039

Reg Uncompressing Tab in the Data

Unforturnately the regex function does not consider A0 as a blank.

Super User
Posts: 10,023

Re: Reg Uncompressing Tab in the Data

Tom. How about:

data _null_;
  id='A'||'09'x||'A0'x||' A';
  put id hex.;
  id=compbl(prxchange('s/[\s\xA0]/ /o', -1, id));
  put id hex.;
run;


data _null_;
  id='A'||'09'x||'A0'x||' A';
  put id hex.;
  id=compbl(prxchange('s/\s|[[:^ascii:]]/ /o', -1, id));
  put id hex.;
run;

Ksharp

Contributor
Posts: 66

Re: Reg Uncompressing Tab in the Data

HI sharp how can we write this prxcahnge any information to write that

Super User
Posts: 10,023

Re: Reg Uncompressing Tab in the Data

What did you mean? I can't understand your words.

Ksharp

Respected Advisor
Posts: 4,173

Re: Reg Uncompressing Tab in the Data

Interesting - it worked with my Win7 SAS9.3 version.

1    data _null_;
2      id='A'||'09'x||'0A'x||' A';
3      put id hex.;
4      id=compbl(prxchange('s/\s/ /o', -1, id));
5      put id hex.;
6    run;

41090A2041
4120412020

But as Ksharp demonstrates it's only about little tweaks to the RegEx if it doesn't do what you need.

Super User
Posts: 10,023

Re: Reg Uncompressing Tab in the Data

Patrick.

Tom means 'A0'x   not '0A'x

Ask a Question
Discussion stats
  • 13 replies
  • 451 views
  • 6 likes
  • 4 in conversation