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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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;

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
novinosrin
Tourmaline | Level 20

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?

Kow
Obsidian | Level 7 Kow
Obsidian | Level 7

Yes, I note the syntax error and have corrected it.

novinosrin
Tourmaline | Level 20

well in that case, like @PaigeMiller pointed out, it should work fine. If not, please post us the log report. Thanks

Kow
Obsidian | Level 7 Kow
Obsidian | Level 7
Here's a log. Looking at it it seems obvious that the call to &var1 will resolve to "Mytitle" and fail in the print var statement since there is no variable called MyTitle. 
 
This problem occurred to me when I was thinking about how to get the value of a macro variable into the PROC PRINT. Typically, I just reference the value in a TITLE statement and that often covers it. But what if I want the value directly in the PROC PRINT itself? For example,  output to a PDF ?
 
You could use : 
 
Data ;
     DataStepTitle  =symget('Var1' );  **syntax is no ampersand and in quotes**;
 
and then
     proc print ;
         VAR DataStepTitle ;    should then print the value MyTitle as wanted.
 
but I was looking and wondering if there is an alternate way, something along the lines of
      VAR "&title1"    -- which doesn't work.
 
 
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73
74 %let var1 = Mytitle ;
75
76 data books ;
77 author = "KOWHEAD" ;
78 publisher = "Cow Books" ;
79 date = 2018 ;
80
81
 
NOTE: The data set WORK.BOOKS has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
 
82 proc print ;
83 var &var1 author publisher date ;
ERROR: Variable MYTITLE not found.
84 title "Case example: var1 is &var1" ;
85
86 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
99

 

 

 

 

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Kow
Obsidian | Level 7 Kow
Obsidian | Level 7

Well, I have the solution by using SYMGET and it works fine.

 

 

Here's a working example of what I am wondering :

 

 
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73
74 %let PassTitle = MyNewTitle ;
75
76 data books ;
77 author = "KOWHEAD" ;
78 publisher = "Cow Books" ;
79 date = 2018 ;
80 D_PassTitle = symget('PassTitle') ;     **solution passing macro value to data variable works fine**;
81
82
 
NOTE: The data set WORK.BOOKS has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
 
83 proc print ;
84     var D_PassTitle author publisher date ;
85      title "Case example: PassTitle is &PassTitle" ;
86
87 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
100

 

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)

Tom
Super User Tom
Super User

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
ChrisNZ
Tourmaline | Level 20

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;

 

 

ChrisNZ
Tourmaline | Level 20

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;
Kow
Obsidian | Level 7 Kow
Obsidian | Level 7

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 11 replies
  • 1524 views
  • 2 likes
  • 5 in conversation