BookmarkSubscribeRSS Feed
fd2010
Calcite | Level 5
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.
5 REPLIES 5
chang_y_chung_hotmail_com
Obsidian | Level 7

   %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


   --*;

fd2010
Calcite | Level 5
Thank you very much for the advice!

%qtrim(%superq()) solved the problem for me!
data_null__
Jade | Level 19
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]
chang_y_chung_hotmail_com
Obsidian | Level 7
@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


   --*;

data_null__
Jade | Level 19
I see now. I guess it's QTRIM that prevents the message about &M.

As long as you keep %UNQUOTE away.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 5 replies
  • 1761 views
  • 0 likes
  • 3 in conversation