BookmarkSubscribeRSS Feed
Filipvdr
Pyrite | Level 9

Hello, i'm using DI studio to output a text file:

 

data _null_; 
   set &SYSLAST; 
      attrib WKT1 length = $32767; 
      attrib WKT2 length = $32767; 
      attrib WKT3 length = $32767; 
      attrib WKT4 length = $32767; 
      attrib WKT5 length = $32767; 
      attrib WKT6 length = $32767; 
      attrib WKT7 length = $32767; 
      attrib WKT8 length = $32767; 
      attrib WKT9 length = $32767; 
      attrib WKT10 length = $32767; 
      attrib WKT11 length = $32767; 
      attrib WKT12 length = $32767; 
      attrib id length = $30; 
      attrib startdate length = $100; 
      attrib enddate length = $20; 
      attrib year length = $40; 
      attrib municipality length = $60; 
      attrib activity length = $60; 
      attrib status length = $30; 
      attrib pilot length = $100; 
      attrib synductisid length = $40; 
      attrib statussynductis length = $30; 
      attrib gipodid length = $60; 
      file "&aa_path.reload.municipalinitiatives.&ts..del" lrecl = 400000 dlm='|';
      
      if ( _n_ = 1 ) then 
         do;
            put 
               "WKT1|WKT2|WKT3|WKT4|WKT5|WKT6|WKT7|WKT8|WKT9|WKT10|WKT11|WKT12|id|startdate|enddate|year|municipality|activity|status|pilot|synductisid|statussynductis|gipodid";
               end;
   put 
      WKT1
      WKT2
      WKT3
      WKT4
      WKT5
      WKT6
      WKT7
      WKT8
      WKT9
      WKT10
      WKT11
      WKT12
      id
      startdate
      enddate
      year
      municipality
      activity
      status
      pilot
      synductisid
      statussynductis
      gipodid
      ;

run; 

When i'm outputting the file, it goes to another tool which will concatenates all o f the WKT fields.

 

When one of the WKT fields has a . as 7th character I want to start it with a space.

 

data &_output1;
set &_input1;
if index(WKT2,'.') = 7 then WKT2 = " " || strip(WKT2);
if index(WKT3,'.') = 7 then WKT3 = " " || strip(WKT3);
if index(WKT4,'.') = 7 then WKT4 = " " || strip(WKT4);
if index(WKT5,'.') = 7 then WKT5 = " " || strip(WKT5);
if index(WKT6,'.') = 7 then WKT6 = " " || strip(WKT6);
if index(WKT7,'.') = 7 then WKT7 = " " || strip(WKT7);
if index(WKT8,'.') = 7 then WKT8 = " " || strip(WKT8);
if index(WKT9,'.') = 7 then WKT9 = " " || strip(WKT9);
if index(WKT10,'.') = 7 then WKT10 = " " || strip(WT10);
if index(WKT11,'.') = 7 then WKT11 = " " || strip(WKT11); 
if index(WKT12,'.') = 7 then WKT12 = " " || strip(WKT12);
run;

The problem is that i'm not seeing the " " in my output file. I tried with "BLA" instead of " " and that works..

 

 

7 REPLIES 7
Patrick
Opal | Level 21

@Filipvdr 

This has very likely nothing to do with DIS but how you write the text file.

What is this " it goes to another tool which will concatenates all o f the WKT fields". Some "tools" SAS inclusive left align strings for printing.

 

One thing you could try: Define for all your WKT fields a permanent $CHAR format as this will more likely maintain the leading blanks.

attrib WKT1 length = $32767 format=$CHAR32767.;
Filipvdr
Pyrite | Level 9

Hi Patrick, i tried your suggestion but same result...

 

If i insert a tab '09'x it works.. but when inserting '20'x it doesn't give me a space in the output text file.. 

 

data &_output1;
 set &_input1;
 if index(WKT2,'.') = 7 then WKT2 = '09'x || strip(WKT2);
 if index(WKT3,'.') = 7 then WKT3 = '09'x || strip(WKT3);
 if index(WKT4,'.') = 7 then WKT4 = '09'x || strip(WKT4);
 if index(WKT5,'.') = 7 then WKT5 = '09'x || strip(WKT5);
 if index(WKT6,'.') = 7 then WKT6 = '09'x || strip(WKT6);
 if index(WKT7,'.') = 7 then WKT7 = '09'x || strip(WKT7);
 if index(WKT8,'.') = 7 then WKT8 = '09'x || strip(WKT8);
 if index(WKT9,'.') = 7 then WKT9 = '09'x || strip(WKT9);
 if index(WKT10,'.') = 7 then WKT10 = '09'x || strip(WT10);
 if index(WKT11,'.') = 7 then WKT11 = '09'x || strip(WKT11); 
 if index(WKT12,'.') = 7 then WKT12 = '09'x || strip(WKT12);
run;
Patrick
Opal | Level 21

@Filipvdr 

The cause is as described here: http://support.sas.com/kb/4/368.html which is just normal SAS behaviour.

 

Code as below could give you what you're after.

data _null_;
  file print;
  array wkt {3} $32767 (3*'123456789');
  WKT2='123456.89';

  do _i=1 to dim(wkt);
    _mv=ifn(substrn(wkt[_i],7,1)='.',1,0);
    put +(_mv) wkt[_i] @;
  end;

  stop;
run;

 

Tom
Super User Tom
Super User

Can you explain what the overall goal of this process is?

It looks like you have split an extremely long value into 12 long values.  Are you then trying to piece them back together?

How were the 12 pieces created in the first place?  Is it possible that any of the pieces ended up being created with leading and/or trailing blanks?  And in the later processing do you need to preserve those blanks?  

 

If you want the PUT statement to preserve leading blanks then you need to use either $CHAR or $VARYING format when printing the value.

But then the consumer of the file needs to also preserve the leading spaces and normally when reading a delimited file like what it looks like you are trying to create the leading/trailing spaces on a field are ignored.

 

Tom
Super User Tom
Super User

Does it have to be an actual space?  Can you use some other character that looks like a space?

Like the 'A0'x character that microsoft uses as a non-breaking space?

ballardw
Super User

You may need to pay attention to the possibility of losing data if your existing value is actually 32767 characters after the strip function.

An inserted character at the lead means that the last character of the original value will be lost.

 

data  example;
   length x $ 5;
   x = '12345';
   x = 'A'||strip(x);
run;

If this is a possibility you can't correct it by adding a character to the value. I would be tempted to look at use of conditional COLUMN pointer values instead of modifying values.

The following I am testing at position 5 because I'm too lazy to make longer variables for a simple example. Of course change the path and file name to something you can find.

data example;
   input row WKT2 $ WKT3 $ ;
   file "c:\path\junk.txt" dlm='|';
   put row @ ;
   if index(WKT2,'.') = 5 then put +1 wkt2 @; else put wkt2 @;
   if index(WKT3,'.') = 5 then put +1 wkt3 @; else put wkt3 @;
   put ;
datalines;
1 1245678 abcd.fg  
2 1234.678 abc      
3 12.4567 abcdefg.  
run;
ChrisNZ
Tourmaline | Level 20

Just  detail, but the char() function is better suited to this test than the index function

if index(WKT12,'.') = 7

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 728 views
  • 0 likes
  • 5 in conversation