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

Please help...

 

Here is a very basic version of my problem (though what I'm actually trying to do is a lot more complicated, I think this shows my issue). 

 

I have names with a single quote in them, and I can't seem to use them for anything because it messes with SAS.

 

 

data userlist;
infile datalines delimiter=',';
length user $ 20 name $ 50;
input user $ name $;
datalines;
username, user na'me
run;

data _null_;
set userlist;
call symput("user", user);
call symput("name", name);
run;

%put User is &user;
%put Name is %str(&name);

 

This code doesn't actually cause an error for me in SAS EG 7.13 (SAS 9.4), but if you put more code after it you will get an error as it's treating everything after the &name as part of a string.

 

 

I can't for the life of me work out how to get the apostrophe to work, as to use %str I need to put a % by the ', and if I do this by inserting it into the string it doesn't seem to work.

 

The context is that I have names (some of which contain apostrophes) in a data set, and I'm looping through them and using a macro to display a title with their name in it, and a table for that person. I'm trying to pass the apostrophe'd names into the macro to be displayed, but I simply can't work it out.

 

Thanks in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Avoid passing parameters to a macro that might contain quotes, commas etc.

Instead move the program part that derives such data into the macro, or don't use a macro at all:

data userlist;
 infile datalines delimiter=','; 
 length user $ 20 name $ 50;
 input user $ name $;
 datalines;
Alfred,Alf'red
run;

data _null_;
set userlist;
call execute('title "Table for ' !! trim(name) !! '";');
call execute('
  proc print data=sashelp.class;
  where name="' !! trim(user) !! '";
  run;
');
run;

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

You can use &name inside a double quoted string.

 

data userlist;
 infile datalines delimiter=','; 
 length user $ 20 name $ 50;
 input user $ name $;
 datalines;
username, user na'me
run;

data _null_;
 set userlist;
 call symput("user", user);
 call symput("name", name);
run;

title "My name is &name..";
proc print data=userlist; run;
PG
David5
Fluorite | Level 6
Thanks, it seems my actual problem stemmed from passing the value into a macro (not in the simplified code I posted), and I have got around it by getting the name from my data set from inside the macro.
Kurt_Bremser
Super User

Avoid passing parameters to a macro that might contain quotes, commas etc.

Instead move the program part that derives such data into the macro, or don't use a macro at all:

data userlist;
 infile datalines delimiter=','; 
 length user $ 20 name $ 50;
 input user $ name $;
 datalines;
Alfred,Alf'red
run;

data _null_;
set userlist;
call execute('title "Table for ' !! trim(name) !! '";');
call execute('
  proc print data=sashelp.class;
  where name="' !! trim(user) !! '";
  run;
');
run;
David5
Fluorite | Level 6

Avoid passing parameters to a macro that might contain quotes, commas etc.

Instead move the program part that derives such data into the macro, or don't use a macro at all:

Thanks, I got it working by not passing the name into the macro, and instead running a data step in the macro to get the name from my userlist dataset using the username passed in.

 

But surely there is a way to pass in a string with a quote? I think I can pass it through in double quotes ok, but then I can't use it in the title statement. e.g:

 

 

%macro test(user=);
	title "Stuff for &user";
%mend;
%test(user="Na'me");

 

This passes the name in ok, but doesn't work because the title statement ends up with double quotes in the middle. Is there a way to solve this from inside the macro to allow it to use the passed in string?

This is out of curiosity now as I've managed to get my code working (thanks for the help on that!).

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 4201 views
  • 0 likes
  • 3 in conversation