BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GreyJoy
Obsidian | Level 7

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)),'"')
1 ACCEPTED SOLUTION

Accepted Solutions
GreyJoy
Obsidian | Level 7

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. 

View solution in original post

14 REPLIES 14
GreyJoy
Obsidian | Level 7
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.
Kurt_Bremser
Super User

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.

GreyJoy
Obsidian | Level 7

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. 

Kurt_Bremser
Super User

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.

ballardw
Super User

@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
Obsidian | Level 7

@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

Kurt_Bremser
Super User

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.

GreyJoy
Obsidian | Level 7
If you look at your 5th observation, you will see that there is an abundance of space between the last character and the closing quotation mark which is my problem. something is causing all these functions that remove spaces to not work.
If I copy and past the string from sas into the test box here... I get this below.

"F101Q08=""Received botulinum toxin in past 3 months
"""
Kurt_Bremser
Super User

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.

GreyJoy
Obsidian | Level 7
Thank you but, I know the problem. The solution is what I am after.
GreyJoy
Obsidian | Level 7

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. 

Tom
Super User Tom
Super User

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 14 replies
  • 1494 views
  • 1 like
  • 4 in conversation