BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
arodriguez
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

10 REPLIES 10
MadhuKorni
Quartz | Level 8

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Single quotes works:

data _null_;

  put 'Try = this';

run;

arodriguez
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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

Tom
Super User Tom
Super User

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.

arodriguez
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

Ksharp
Super User

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;

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