Your SAS programs, embedded in web apps and elsewhere

Let Blows up when Re-creating Variable with Apostrophe as Value.

Accepted Solution Solved
Reply
Super Contributor
Posts: 497
Accepted Solution

Let Blows up when Re-creating Variable with Apostrophe as Value.

I am using SAS 9.2. 

Is there a way to use the str(%’) syntax when I use the %let statement on a marco?  Detail is below.

First I created max_level_name; by the below code.

SELECT total, &enrRepTy, &degOrStudIDTy into :max_level_total, :max_level_name, :max_deg_Or_stud_id

from

         ( select sum(students_enrolled) as total, &enrRepTy, &degOrStudIDTy

          from enrollment

           &whereClause

          group &enrRepTy, &degOrStudIDTy)

having total = max(total);

I found that when I outputted &max_level_name in a dynamic title statement as

dynamic title2="excluding &max_level_name ";

that SAS has a bug that splits my title text e.g.

Baccalaureate only becomes

Baccalaureate

only

I found a crazy work around by creating a new variable and saving the value into the new variable.  Note there are no trailing or leading spaces in my dataset.

%let displaymaxlevelname = &max_level_name;

My crazy work around worked fine until I changed my selection from Baccalaureate to Master’s adding the apostrophe.  Where it blows up due to a Literal contains unmatched quote error.  Is there a way to use the str(%’) syntax when I use the %let statement on a marco?

Is there a way that I do not have to do the intermediary step?


Accepted Solutions
Solution
‎03-02-2015 12:59 PM
Super User
Super User
Posts: 7,050

Re: Let Blows up when Re-creating Variable with Apostrophe as Value.

Posted in reply to DavidPhillips2

Instead of TRIMMED use SEPARATED BY ' '

You also might want to protect against embedded double quote characters by using QUOTE() function.

title2=%sysfunc(quote(excluding &max_level_name));

View solution in original post


All Replies
Super User
Super User
Posts: 7,050

Re: Let Blows up when Re-creating Variable with Apostrophe as Value.

Posted in reply to DavidPhillips2

1) You can remove the leading/trailing spaces by changing your SQL syntax.  If you are using a relative new version of SAS use the TRIMMED option.  For an older version use SEPARATED BY clause.

SELECT total

     , &enrRepTy

     , &degOrStudIDTy

  into :max_level_total trimmed

     , :max_level_name trimmed

     , :max_deg_Or_stud_id trimmed

2) You can use %SUPERQ() to quote the value of your macro variable.


%let max_level_name=%superq(max_level_name) ;



Super Contributor
Posts: 497

Re: Let Blows up when Re-creating Variable with Apostrophe as Value.

I am using SAS 9.2 the feature trimmed does not appear to be in SAS 9.2

If I use %superq it returns to the odd spacing. 

Solution
‎03-02-2015 12:59 PM
Super User
Super User
Posts: 7,050

Re: Let Blows up when Re-creating Variable with Apostrophe as Value.

Posted in reply to DavidPhillips2

Instead of TRIMMED use SEPARATED BY ' '

You also might want to protect against embedded double quote characters by using QUOTE() function.

title2=%sysfunc(quote(excluding &max_level_name));

Super Contributor
Posts: 497

Re: Let Blows up when Re-creating Variable with Apostrophe as Value.

Separated by ‘’ solved it. 

When I used title2=%sysfunc(quote(excluding &max_level_name)); it blew up.

Super User
Super User
Posts: 7,050

Re: Let Blows up when Re-creating Variable with Apostrophe as Value.

Posted in reply to DavidPhillips2

Did you have the line to quote the value with %SUPERQ() to protect the unbalanced quotes?  If not you could include here:

title2=%sysfunc(quote(excluding %superq(max_level_name)));

Super Contributor
Posts: 497

Re: Let Blows up when Re-creating Variable with Apostrophe as Value.

No, I only used separated by ‘ ‘.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 465 views
  • 3 likes
  • 2 in conversation