Hi everyone,
Today I was producing a output with a data step, with the classical put statement to print results on TXT file. The problem appear when I had tried to print a variable with an eq character.
I know that put could do things as
put name=;
but if it is enclosed with quotes, it should print the content of the quotes.
Any idea of how to print this?
data test;
set try;
put "Try = this";
run;
Thanks
If you want to reference the value of a macro variable then you need to use & before the macro variable name. If you want to use the reference inside of a quoted string then the outer quotes need to be double quote character and not single quote character.
%let mvar=TRY = This;
data _null_;
put "&mvar" ;
run;
If the value of the macro variable could contain embedded double quote characters then you will need to protect them or the generated code will look like two strings instead of one. So in this example the first PUT statement is wrong since it ends up looking like two quoted strings separated by a reference to a variable named THIS.
%let mvar=TRY = " This " is quoted;
data _null_;
put "&mvar" ;
put %sysfunc(quote(&mvar));
run;
NOTE: Variable This is uninitialized.
TRY = . is quoted
TRY = " This " is quoted
data Have;
var = "This";
run;
data _null_;
set have;
put "Try = " var;
run;
OR
data Want;
var= "This";
put "Try =" var;
run;
Displays the value in log.
data _null_;
file "path\filename.txt";
var= "This";
put "Try =" var;
run;
Displays the value in the file.
Single quotes works:
data _null_;
put 'Try = this';
run;
and if the "=" is in a variable macro, how can I do it?
If you want to reference the value of a macro variable then you need to use & before the macro variable name. If you want to use the reference inside of a quoted string then the outer quotes need to be double quote character and not single quote character.
%let mvar=TRY = This;
data _null_;
put "&mvar" ;
run;
If the value of the macro variable could contain embedded double quote characters then you will need to protect them or the generated code will look like two strings instead of one. So in this example the first PUT statement is wrong since it ends up looking like two quoted strings separated by a reference to a variable named THIS.
%let mvar=TRY = " This " is quoted;
data _null_;
put "&mvar" ;
put %sysfunc(quote(&mvar));
run;
NOTE: Variable This is uninitialized.
TRY = . is quoted
TRY = " This " is quoted
What are actually trying to produce? The data step you posted will output the 10 character string "Try = this" once for each observation in the input dataset TRY.
I have a macro that produce a report.
Until now i have any problem with this, but today i had use a var with a column name with an equal.
The data step do for each break page print the "headers" and with an equal it doesn't work.
The call is something like
if first.page then do;
put @%eval(col_width) "%trim(&header)";
end;
where trim is a Rolland macro, that could be find here http://www.datasavantconsulting.com/roland/trim.sas
I have tried the same code without trim and seems to work, so It's no problem of the put statement. I will review for the problem
If you call a macro with code like %XXX(try=THIS) then it looks to SAS like you want to set the parameter TRY to the value THIS.
So you need to quote the value you are passing to the macro call. Here is one way
%trim(%superq(header))
TRIM is the name of a macro supplied by SAS. Did Roland create his own version of that macro?
You may also consider what your code is doing in the first place. I don't see a reason for macro calls and references etc. on a report out procedure?
What is your wanted result in the output file?
Another workaround way in Data Step is using SYMGET().
%let mvar=TRY = " This " is quoted; data _null_; put "&mvar" ; put %sysfunc(quote(&mvar)); x=symget('mvar'); put x run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.