Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting a Variable in a Title Statement 1

Status
Not open for further replies.

ks01

MIS
Aug 11, 2002
110
US
Hi everyone ...

Does anyone know if it's possible to insert a user defined variable from a DATA statement in a TITLE statement?

For example

Code:
DATA TEMP;
  VAR1 = TODAY() - 1;
RUN;

PROC PRINT;
 TITLE1 'REPORT SUMMARIZING VAR1 DATA';
RUN;

I've seen this used with system variables such as &SYSDAT9, &SYSDAY, but I cannot seem to get this to work.

Thanks ...
 
Variables preceded by an & are known as MACRO variables. &SYSDAT9 and &SYSDAY are system defined Macro Variables.
You can easily define a macro variable yourself and use that in the title. There are 2 main ways to define a macro variable. The first is to use the SYMPUT call function, the second is to use the %LET macro function.
Code:
data _null_;
  VAR1 = TODAY() - 1;
  call symput('REPDATE',VAR1);
run;

title "blah blah blah &REPDATE blah";

or

Code:
%let rundt = %sysfunc(date(),date9.);

title "blah blah blah &rundt blah";


Things to note:-
1 - Your title statement needs to be enclosed in double quotes rather than single quotes in order for the macro variable to be substituted (that's not the correct word for it, but my mind suddenly went blank). Single quotes denotes a "literal string".

2 - When creating a datastep as you've done, which doesn't require and output dataset, you can use the _NULL_ keyword for the output dataset name, this results in no dataset being created.

3 - The title statement does not need to be in the procedure it is being used in, it can be in "open code" before the step.

Enjoy.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thanks Chris!

I'm still learning SAS and haven't gotten to macros yet in the Step-by-Step SAS guide yet.

I tried playing around with the %LET and CALL SYMPUT statements, but couldn't get them to work thinking it was my own lack of understanding that was the problem.

Would you mind explaining why my format doesn't affect the title, even when I put the FORMAT statement in PROC PRINT?

Code:
data data;
  format yesterday downame.;
  yesterday = today()-1;
  call symput('REPDATE',yesterday);
run;

proc print;
  title "Report Date: &REPDATE";
run;

Thanks ...

K
 
OK, the macro variables only hold TEXT, no matter what you put in there, SAS treats it as text. In order to format your date field (sorry, I should have thought of that) you need to change the data before putting it into the macro variable:-
Code:
data _null_;
  *format yesterday downame.;
  length yesterday $20;
  yesterday = put(today()-1,downame.);
  call symput('REPDATE',yesterday);
run;
The way that this is different from what you did is this, a FORMAT applied to a variable as you did in your step, changes the way it is displayed, but not how it is stored. This means that when you load this variable into the macro variable, it takes the underlying date value (a count of the number of days since 01/01/1960) rather than the formatted valule that gets displayed.
Using the PUT function I've included above actually create a new text string containing the formatted value, rather than the underlying data.
I hope that makes it clear, let me know if not. Getting to grips with formats and data can be a little tricky sometimes.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Hi Chris ...

Thanks for the quick response.

That makes perfect sense! Trying to get a grasp on all of the formats and informats that are available is a bit overwhelming. I'm still hoping that one day I'll find a "cheat sheet" type of reference. It took me about 30 minutes to find DOWNAME and had almost given up.

Much appreciated and thank you again!

K
 
The online doco is pretty good for that sort of thing:-
Formats are listed under
Base SAS > SAS Language Reference: Dictionary > Dictionary of Language Elements > Formats
On the formats page you can select "Formats by Category". It's immensely helpful for tracking down formats for most situations.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Hi Chris ...

One last thing, I noticed you added an asterisk to the beginning of your format statement.

I haven't seen this done before and checked the reference dictionary and was unable to find this. It worked with and without the asterisk.

Mind sharing the significance of the asterisk?

Thanks ...

Kent
 
An asterisk at the start of a line comments it out so it doesn't get run. There are 2 methods of commenting in regular code. Prefixing with an asterisk (remember to end with a semi-colon still to tell SAS that the comment has ended).
The second is to wrap the text to be commented in
/* This is a comment. */
* This is also a comment ;

Enjoy.


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top