SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tburus
Obsidian | Level 7

I am trying to write a program that loops over a list of names to create multiple PDFs. It's working fine except that one of the names I need it to loop over contains an apostrophe (single quote). I'd rather not just remove the apostrophe from my data, but can't figure out how to get SAS to ignore it in a string literal. Below is my current code. The problem name occurs as one of the &conf variables that I'm looping over.

 

*Run report for each conference;
%macro runcp;
  %do i=70 %to &nconfs.;
    %let conf_name = &&conf&i;
	%let div_num = &&divs&i;
	%cpreport;
  %end;
%mend runcp;
%runcp;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

But you don't have any string literals in your posted code.

It looks like your code is referencing a series of macro variables with numeric suffix on their name.  If those macro variables were not created with macro quoting then add it when you create the macro variables in your code.

%macro runcp;
%local i conf_name div_num;
%if %symexist(nconfs) %then %do i=70 %to &nconfs.;  
  %let conf_name = %superq(conf&i);
  %let div_num = %superq(divs&i);
  %cpreport;
%end;
%else %put ERROR: Macro &sysmacroname requires NCONFS to exist.;
%mend runcp;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

But you don't have any string literals in your posted code.

It looks like your code is referencing a series of macro variables with numeric suffix on their name.  If those macro variables were not created with macro quoting then add it when you create the macro variables in your code.

%macro runcp;
%local i conf_name div_num;
%if %symexist(nconfs) %then %do i=70 %to &nconfs.;  
  %let conf_name = %superq(conf&i);
  %let div_num = %superq(divs&i);
  %cpreport;
%end;
%else %put ERROR: Macro &sysmacroname requires NCONFS to exist.;
%mend runcp;
tburus
Obsidian | Level 7

The string literals occur in my &conf&i macro variables. In particular, conf73 = Presidents' Athletic Conference. This is the one I need to get working. I replaced &&conf&i with %superq(conf&i) as you suggested and it appears to have worked correctly. Thanks.

Tom
Super User Tom
Super User

In SAS code a string literal is enclosed in quotes, like this:

name = "Presidents' Athletic Conference";

So using your macro variable to generate a string literal shouldn't cause any trouble.

name = "&conf73";

because of the outer quotes.

But trying to assign it to another macro variable without adding either actual quotes or macro quoting will cause issues.  Try using one of these methods to add macro quoting.

%let x=%bquote(&conf73);
%let x=%superq(conf73);

 

tburus
Obsidian | Level 7
Thanks for the info. This is very helpful. I'm still getting around the learning curve on some of the higher level functions within SAS.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1758 views
  • 0 likes
  • 2 in conversation