DATA Step, Macro, Functions and more

macro function to trim trailing blanks with unblanced quote in text string

Reply
Occasional Contributor
Posts: 14

macro function to trim trailing blanks with unblanced quote in text string

I need to create macro variables with a text string that is stored in a sas data set. The text string can be of variable length. Once I create the macro variable, I need to trim the trailing blanks. But if the text string contains unblanced quote, the %trim and %compres do not work.

Any advice would be appreciated.

/*********Sample code************/
data test;
length title $200;
title="Summary of King's College VascuQoL Domain Results by Visit";
output;
run;

proc sql noprint;
select title into:tbltit from test;
quit;

%put tbltit=%nrbquote(&tbltit);

%let tbltit=%cmpres(%nrbquote(&tbltit));

%put tbltit=%nrbquote(&tbltit);
/*********End of Sample code************/

/**************Log ***********************/

1
2 data test;
3 length title $200;
4 title="Summary of King's College VascuQoL Domain Results by Visit";
5 output;
6 run;

NOTE: The data set WORK.TEST has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


7
8 proc sql noprint;
9 select title into:tbltit from test;
10 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


11
12 %put tbltit=%nrbquote(&tbltit);
tbltit=Summary of King's College VascuQoL Domain Results by Visit

13
14 %let tbltit=%cmpres(%nrbquote(&tbltit));
15
16 %put tbltit=%nrbquote(&tbltit);

/**************End of Log ***********************/

At line 16, SAS pretty much stopped working.
Regular Contributor
Posts: 241

Re: macro function to trim trailing blanks with unblanced quote in text string

   %let tbltit=;


   data _null_;


      length title $200;


      title="Summary of King's College VascuQoL Domain Results by Visit";


      call symputx('tbltit', title, 'g');


   run;


 


   %*-- trim and quote --*;


   %let tbltit=%qtrim(%superq(tbltit));


 


   %*-- check --*;


   title "&tbltit";


   proc freq data=sashelp.class;


      tables age;


   run;


   title;


   %*-- on lst


   Summary of King's College VascuQoL Domain Results by Visit


   The FREQ Procedure


                                   Cumulative    Cumulative


   Age    Frequency     Percent     Frequency      Percent


   --------------------------------------------------------


    11           2       10.53             2        10.53


    12           5       26.32             7        36.84


    13           3       15.79            10        52.63


    14           4       21.05            14        73.68


    15           4       21.05            18        94.74


    16           1        5.26            19       100.00


   --*;

Occasional Contributor
Posts: 14

Re: macro function to trim trailing blanks with unblanced quote in text string

Thank you very much for the advice!

%qtrim(%superq()) solved the problem for me!
Respected Advisor
Posts: 3,777

Re: macro function to trim trailing blanks with unblanced quote in text string

Do you think there is any advantage to creating the macro variable with the QUOTES using QUOTE function. Using QUOTE function imbedded unmatched quotes are handled properly. Also, using "separated by" the extra blanks are trimmed.

[pre]
data test;
length title $200;
title='Summary of King"s College VascuQoL Domain Results by Visit';
output;
run;

proc sql noprint;
select quote(strip(title)) into:tbltit separated by ' '
from test;
quit;
%put NOTE: ***&tbltit***;

title &tbltit;
proc freq data=sashelp.class;
tables age;
run;

** On log
6310 %put NOTE: ***&tbltit***;
NOTE: ***"Summary of King""s College VascuQoL Domain Results by Visit"***



[/pre]
Regular Contributor
Posts: 241

Re: macro function to trim trailing blanks with unblanced quote in text string

@data _null_; Nice! I did write it first time using quote() function in the data step. But then I changed my mind because macro quoting seems a tiny bit safer due to possible macro triggers in the title. Maybe I shouldn't have. :-)



   %let qTitle=;


   data _null_;


      length title $200;


      title='Summary of M&M Consumption at SAS''s campus';


      call symputx('qTitle', quote(strip(title)), 'g');


   run;


 


   %*-- check --*;


   title &qTitle;


   proc sql


      select 'a lot' as total from sashelp.class(obs=1); 


   quit;


   title;


 


   %*-- on lst


   WARNING: Apparent symbolic reference M not resolved.


   ...


   Summary of M&M Consumption at SAS's campus


   total


   -----


   a lot


   --*;

Respected Advisor
Posts: 3,777

Re: macro function to trim trailing blanks with unblanced quote in text string

I see now. I guess it's QTRIM that prevents the message about &M.

As long as you keep %UNQUOTE away.
Ask a Question
Discussion stats
  • 5 replies
  • 279 views
  • 0 likes
  • 3 in conversation