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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

6 REPLIES 6
Tom
Super User Tom
Super User

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) ;



DavidPhillips2
Rhodochrosite | Level 12

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. 

Tom
Super User Tom
Super User

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));

DavidPhillips2
Rhodochrosite | Level 12

Separated by ‘’ solved it. 

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

Tom
Super User Tom
Super User

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)));

DavidPhillips2
Rhodochrosite | Level 12

No, I only used separated by ‘ ‘.

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
  • 6 replies
  • 1070 views
  • 3 likes
  • 2 in conversation