Obs | D_PassTitle | author | publisher | date |
---|---|---|---|---|
1 | MyNewTitle | KOWHEAD | Cow Books | 2018 <<<<The data Passed Macro Variable is now in the output |
Suppose I have the following (as a case example):
%let title = MyTitle ;
and a data set called BOOKS with some variables.
Now I do this:
proc print data=books ;
var &title author year publisher ;
title1 "Macro var resolves &title" ;
The macro var will resolve correctly in the Title1 statement. However, the use of &title in the VAR statement causes an error.
I tried put(&title) or quotes around &title in the VAR statement , etc. but no work.
If the VAR references an existing macro variable it WILL work.
%let mvar1 = MyTitle ;
proc print ;
var &mvar1 ;
will work fine.
I could use
datastep;_
title = SYMGET(&title) ;
to pass it to the dataset but I would like to see if there is a more direct way with the VAR statement itself.
Any discussion of pros/cons appreciated.
The VAR statement can only take variable names as parameters.
but SQL can print anything: [edited: sorry about the typo]
proc sql ;
title "Case example: var1 is &var1" ;
select "&var1", author, publisher, date from TABLE;
quit;
This makes little sense that you are trying to tell SAS to use a variable named "MyTitle", there is no such thing as a variable named "MyTitle". Why? Becuase variable names do not have quotes or doublequotes in them. On the other hand, if you use
%let title=mytitle;
without any quotes, and you have such a variable in the data set, then it should work.
is this part of your code correct in the first place?
&title = "My Title " ;
or one should assume you have an existing macro var by the name title?
Yes, I note the syntax error and have corrected it.
well in that case, like @PaigeMiller pointed out, it should work fine. If not, please post us the log report. Thanks
But what if I want the value directly in the PROC PRINT itself? For example, output to a PDF ?
Do you mean you want the variable to have a LABEL that is the value of the macro variable? Because so far your example indicates that the variable name (not the label) is specified by the macro variable.
Please clarify this.
VAR "&title1" -- which doesn't work.
Yes, because in the VAR statement, you can't use single quotes or double quotes. You can only use variable names (not labels) with no quotes.
Can you show us working code for a single case that does what you want WITHOUT macros? If so, then it should be very easy to convert the working code WITHOUT macros to working code WITH macros ... but you have to do the first step of creating working code WITHOUT macros. You can't skip this step.
Well, I have the solution by using SYMGET and it works fine.
Here's a working example of what I am wondering :
Output is produced correctly:
Case example: PassTitle is MyNewTitle <<<resolves correctly in title ;
Obs | D_PassTitle | author | publisher | date |
---|---|---|---|---|
1 | MyNewTitle | KOWHEAD | Cow Books | 2018 <<<<The data Passed Macro Variable is now in the output |
The question is there a simpler/alternate way to directly reference the macro variable in the VAR statement and have it resolve correctly without the need for the SYMGET. For example, suppose the dataset was locked and I could not change it I need to process thru some macro loop and have the macro value printed row by row alongside the data listing. An intermediate data step would work but I was wondering if there might be some other syntax I could use that would allow this.
i.e VAR &PassTitle Author pub date ( in some syntactical form that resolves it correctly)
You cannot add a variable to your data set without changing your data set.
You could create NEW dataset and print that.
%let title=Any Free Text ;
data to_print;
_title_="&title";
set sashelp.class (obs=4);
run;
title1 "I added the column _TITLE_ with the value &title";
proc print data=to_print;
run;
Results
I added the column _TITLE_ with the value Any Free Text Obs _title_ Name Sex Age Height Weight 1 Any Free Text Alfred M 14 69.0 112.5 2 Any Free Text Alice F 13 56.5 84.0 3 Any Free Text Barbara F 13 65.3 98.0 4 Any Free Text Carol F 14 62.8 102.5
You could create NEW dataset and print that.
or a view:
%let title=Any Free Text ;
data to_print/view=to_print;
_title_="&title";
set sashelp.class (obs=4);
run;
title1 "I added the column _TITLE_ with the value &title";
proc print data=to_print;
run;
The VAR statement can only take variable names as parameters.
but SQL can print anything: [edited: sorry about the typo]
proc sql ;
title "Case example: var1 is &var1" ;
select "&var1", author, publisher, date from TABLE;
quit;
The SQL trick is a good one! I was unaware that SQL statements could be used in SAS code like this, that opens up a lot of ideas.
I think this one wins the prize, using an intermediate data step will obviously work as well. If I needed a solution that's what I would probably wind up using.
The SQL solution is the kind of thing I was looking for and caps the learning experience for this question. Thanks to all contributors.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.