At my office SAS datasets are updated and made available at 5:00 am every morning. Each project has this dataset but this is the only one...so far that is giving me this problem. Before I take it to IT and the Data Managers I want to see if there is something I am missing.
As you will see in the dataset I provided I am concatenating 3 variables into one to provided the necessary text that will fit into a Label statement. The problem is that the Label ends up looking like...
"THIS IS THE LABLE " - and I can not get rid of all this blank space. I have tried trim(), Cat, Cats, Catt, Catx, Compress, Substr(Zfieldnm,1,Length(zfieldnm). If anyone can take a look at the file and tell me how you can correctly manipulate the string I would be grateful.
catx("","F",zcrfnb,zfieldnb,'="',zfieldnm,'"')
catx("","F",zcrfnb,zfieldnb,'="',trim(zfieldnm),'"')
"F"||zcrfnb||zfieldnb||'="'||trim(zfieldnm)||'"'
catx("","F",zcrfnb,zfieldnb,'="',substr(zfieldnm,1,length(zfieldnm)),'"')
UPDATE 2: Figured it out!!
trim(compress(zfieldnm,,'kw'));
The compress( , , 'kw') works by (k) keeping the characters in the string, while (w) specifies only the printable characters. This removes the special unseen characters that cause Trim to not work. Trim then removes the trailing blanks leaving me with the Label name without unnecessary spaces.
Since I have no idea what your data looks like, I had to make up some:
data test;
zcrfnb = "AAA ";
zfieldnb = "BBB ";
zfieldnm = "XXX ";
want = catx("","F",zcrfnb,zfieldnb,'="',zfieldnm,'"');
run;
Please tell us what you expect instead of
F AAA BBB =" XXX "
or provide us with some real data of yours.
Please re-read my post. I make clear mention of the data I have provided and it is present in the downloadable zip file. You will find a SAS dataset named labels.
Take a VERY CLOSE look at your data. It might be that the trimming does not work because you have some non-blank, but undisplayable character at the end of your variable.
So, if your variable was defined as $100, do this:
target = put(source,$hex200.);
(use double the defined length in the format)
And then inspect the hex string. Anything else but "20"x will disturb the automatic trimming of CATX and similar functions.
@GreyJoy wrote:
catx("","F",zcrfnb,zfieldnb,'="',zfieldnm,'"')
catx("","F",zcrfnb,zfieldnb,'="',trim(zfieldnm),'"')
"F"||zcrfnb||zfieldnb||'="'||trim(zfieldnm)||'"'
catx("","F",zcrfnb,zfieldnb,'="',substr(zfieldnm,1,length(zfieldnm)),'"')
I could keep going with all the different iteration and combinations for what i have done. Just substitute catx with the other cat function or compress.
Isn't really working code or a complete statement. There is no assignment to a variable or a semicolon to end any statement.
@GreyJoy wrote:At my office SAS datasets are updated and made available at 5:00 am every morning. Each project has this dataset but this is the only one...so far that is giving me this problem. Before I take it to IT and the Data Managers I want to see if there is something I am missing.
As you will see in the dataset I provided I am concatenating 3 variables into one to provided the necessary text that will fit into a Label statement. The problem is that the Label ends up looking like...
"THIS IS THE LABLE " - and I can not get rid of all this blank space. I have tried trim(), Cat, Cats, Catt, Catx, Compress, Substr(Zfieldnm,1,Length(zfieldnm). If anyone can take a look at the file and tell me how you can correctly manipulate the string I would be grateful.
catx("","F",zcrfnb,zfieldnb,'="',zfieldnm,'"') catx("","F",zcrfnb,zfieldnb,'="',trim(zfieldnm),'"') "F"||zcrfnb||zfieldnb||'="'||trim(zfieldnm)||'"' catx("","F",zcrfnb,zfieldnb,'="',substr(zfieldnm,1,length(zfieldnm)),'"')
UPDATE: Compress() does the job at removing the blank spaces at the end but I still need to keep the blanks between the words in the Label
I downloaded your dataset to my myfolders library, and ran this:
data test;
set myfold.labels (keep=zcrfnb zfieldnb zfieldnm);
length want $300;
want = cats("F",zcrfnb,zfieldnb,'="',substr(zfieldnm,1,256),'"');
run;
Note that you need the SUBSTR because the maximum length of a SAS variable label is 256.
The incriminated string is in fact in the 10th observation, and the blanks are caused by special characters:
data check;
set myfold.labels (keep=zfieldnm);
checkvar = put(zfieldnm,$hex100.);
run;
proc print data=check (obs=10 firstobs=10);
var checkvar;
run;
Result:
10 526563656976656420626F74756C696E756D20746F78696E20696E20706173742033206D6F6E7468730D0A20202020202020
This sequence corresponds to the end of the string:
730D0A20
0D0A is the Windows CRLF. You need to clean your data of such characters.
COMPRESS the hex 0D0A out of the string.
zfieldnm = compress(zfieldnm,'0d0a'x);
UPDATE 2: Figured it out!!
trim(compress(zfieldnm,,'kw'));
The compress( , , 'kw') works by (k) keeping the characters in the string, while (w) specifies only the printable characters. This removes the special unseen characters that cause Trim to not work. Trim then removes the trailing blanks leaving me with the Label name without unnecessary spaces.
So that is just removing these characters (at least with encoding=wlatin1).
000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
7F
81
8D
8F
90
9D
AD
It leaves many other potentially spurious characters including the common one 'A0'x that many microsoft applications treat as a non-breaking space.
Also not clear how it will work with encoding=utf-8
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.