Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extra spaces in concatenated strings

Status
Not open for further replies.

LarrySteele

Programmer
May 18, 2004
318
US
Platform: SAS 9.1.3 on z/OS.

I'm trying to create a simple report title. The text should read: "EMPLOYEE POP AS OF YEAR END {last year}". Here's what I've done so far to create this:

[tt]%GLOBAL YR;
DATA _NULL_;
CALL SYMPUT('YR',YEAR(TODAY()) -1);
RUN;
[/tt]

In my Proc Tab section, I have:
[tt]TITLE 'EMPLOYEE POP AS OF YEAR END '&YR;[/tt]

Here is what actually displays on the output:
[tt]EMPLOYEE POP AS OF YEAR END 2006
[/tt]

I would like to remove the extra spaces between 'END' and '2006'. After searching here, SAS documentation, and Google, I've gather this together:

[tt]%GLOBAL YR;
%GLOBAL TITLETEXT;
DATA _NULL_;
CALL SYMPUT('YR',YEAR(TODAY()) -1);
CALL SYMPUT('TITLETEXT',
TRIM('EMPLOYEE POP AS OF YEAR END) ||
TRIM(YEAR(TODAY()) -1));
RUN;[/tt]

Then in my Proc Tab section, I put:
[tt]TITLE EMPLOYEE POP AS OF YEAR END '&YR;[/tt]

The output of this version is the same as above - I get extra spaces between the string and sas variable. I've wrapped both in trim functions and yet these spaces are still there.

Any suggestion on how to get rid of these spaces?

TIA,
Larry

 
Copy & paste error on my part.

The second Proc Tab scenario should be:

Then in my Proc Tab section, I put:
[tt]TITLE &TITLETEXT;[/tt]

Sorry if that causes any confusion.

- Larry
 
Solved. Apparently, Trim() does not trim leading/trailing spaces, rather it performs an RTrim only. To trim both leading/trailing spaces, use the Strip() function:

[tt]
%GLOBAL TITLETEXT;
DATA _NULL_;
CALL SYMPUT('TITLETEXT',
TRIM('EMPLOYEE POP AS OF YEAR END) ' ||
TRIM(YEAR(TODAY()) -1));
RUN;
[/tt]

Now my &TITLETEXT does not include those extra spaces.

- Larry
 
Good work Larry, thanks for posting the solution too!

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thanks Chris.

I just learned another way to do this... Use SYMPUTX rather than SYMPUT. So, my first code snippet would be:
[tt]%GLOBAL YR;
DATA _NULL_;
CALL SYMPUTX('YR',YEAR(TODAY()) -1);
RUN;
[/tt]
SYMPUTX does the same thing as SYMPUT, but also trims leading and trailing spaces.
 
Larry,
FYI - Those two functions don't exist in SAS 8. You can use the trim() and the left() function together to get the same results.

Klaz
 
Thanks Klaz, definitely good to know. I've been through the frustration of trying to implement a solution I found online only to discover (after much work) that the solution only applies to a version newer than my own. Hopefully you've saved others that aggravation!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top