The SAS Output Delivery System and reporting techniques

Put with eq character

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 144
Accepted Solution

Put with eq character

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


Accepted Solutions
Solution
‎08-20-2015 05:03 AM
Super User
Super User
Posts: 6,317

Re: Put with eq character

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

View solution in original post


All Replies
Contributor
Posts: 65

Re: Put with eq character

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.

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Put with eq character

Single quotes works:

data _null_;

  put 'Try = this';

run;

Frequent Contributor
Posts: 144

Re: Put with eq character

and if the "=" is in a variable macro, how can I do it?

Solution
‎08-20-2015 05:03 AM
Super User
Super User
Posts: 6,317

Re: Put with eq character

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

Super User
Super User
Posts: 6,317

Re: Put with eq character

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.

Frequent Contributor
Posts: 144

Re: Put with eq character

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

Super User
Super User
Posts: 6,317

Re: Put with eq character

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?

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Put with eq character

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?

Esteemed Advisor
Posts: 6,646

Re: Put with eq character

What is your wanted result in the output file?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Grand Advisor
Posts: 9,571

Re: Put with eq character

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;

☑ This topic is SOLVED.

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

Discussion stats
  • 10 replies
  • 667 views
  • 3 likes
  • 6 in conversation