gdogg371
gdogg371

Reputation: 4142

Strip and tranwrd converting trailing spaces to trailing dashes

I am trying to convert a text file name I have read in into a legal dataset name in SAS version 9.0.

The code I am using is this (where fnames is a dataset containing text file names):

data fnames;
set fnames;
filename2 = scan(filename, 1, '.');
filename3 = tranwrd(filename2, "_", "-");
filename4 = strip(filename3);
filename5 = tranwrd(filename4, " ", "-");
/*filename4 = compress(filename3);*/
filename4 = cats('_', filename4);
drop filename2 filename3;
run;

I want this to replace any spaces in my file name with a dash, but not the trailing spaces at the end of the file name (field length and format are both $200. but the string lengths themselves are variable. My input looks like this:

1080528-19-08-2016-Man Utd-v-Southampton.txt

...and my output looks like this:

1080528-19-08-2016-Man-Utd-v-Southampton----------------------------------------------------------------------------------------------------------------------------------------------------------------

...when it should look like this:

1080528-19-08-2016-Man-Utd-v-Southampton

Can someone please tell me what I need to change?

Thanks

Upvotes: 1

Views: 489

Answers (1)

Joe
Joe

Reputation: 63424

All SAS character variables contain spaces through to the end of their length. That is unavoidable - so you will need to always work with TRIM every time you translate spaces. You can't work piecemeal quite like you are because of that - the strip doesn't really do much (it left-aligns it if it's not already, but that's it) since it doesn't get rid of those spaces. There is no varchar concept in SAS.

So, this:

filename5 = tranwrd(filename4, " ", "-");

Needs to be:

filename5 = tranwrd(trim(filename4), " ", "-");

At minimum.

I would note that it's confusing why you are translating _ to - when _ is legally part of a SAS dataset name and - is not generally.

Perhaps:

filename_fin = cats('_',translate(scan(filename,1,'.'),'__',' -'));

So:

data _null_;
  length filename $200;
  filename= '1080528-19-08-2016-Man Utd-v-Southampton.txt';
  filename_fin = cats('_',translate(scan(filename,1,'.'),'__',' -'));
  put +4 filename= / filename_fin=;
run;

I would note that this still is probably not a valid memname as it's over 32 characters.

Upvotes: 2

Related Questions