DATA Step, Macro, Functions and more

macro- %let statements

Reply
N/A
Posts: 0

macro- %let statements

I am creating a macro for these bargraphs. There is already a working syntax for it and i am trying to tranform it into a usable macro. With these bargraphs I have to specify the midpoints. I was trying to use a %let to specify the midpoints since they are different with each data set. I understand that the macro reads numbers differently and I understand the use of eval and I dont want to use that. I have midpoint 5 15 20 25. how can I just write the numbers in the %let statement? I tried double quotes"" and they didnt seem to work.

Also for the same graph I want to outgraph to a specified location (also given by a %let statement) what kind of quote marks are used for this??
right now i have;
%let graph= C:/desktop/SAS/graphs;

(syntax) outgraph '&graph';

I am somewhat confused about the "" if anyone knows a good site to explain this I'd love a heads up

Thanks!!
Super Contributor
Posts: 264

Re: macro- %let statements

Hi,

I suggest that you start reading the documentation at http://support.sas.com/documentation/onlinedoc/91pdf/sasdoc_91/base_macro_6997.pdf .

You do not need any quotes when assign a value to a macro variable, if you put the value in quotation marks they are part of the value. If you want to modify a numerical variable you have to use the %eval function. Try the following:
%let five = 5;
%let aTen = &five + &five;
%let bTen = %eval(&five + &five);

%put five = &five;
%put aTen = &aTen;
%put bTen = &bTen;

If you want to insert a macro variables' value in normal sas-code and the sas-code requires quoted text, always use double quotes. The follwing three lines of code show the difference between single and double quotes:

%let graph= C:/desktop/SAS/graphs;
%put graph = "&graph";
%put graph = '&graph';


Andreas
SAS Super FREQ
Posts: 8,744

Re: macro- %let statements

Hi:
Consider this code outside of your specific program needs:
[pre]
options nodate nonumber nocenter;
%let frog = Kermit;
%let show = Sesame Street;
%let fibonacci = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55;


proc print data=sashelp.class(obs=2);
title 'Single quotes: &frog - &show';
title2 "Double quotes: &frog - &show";
title3 "Fibonacci Series: &fibonacci";
run;
[/pre]

The results of running the above program are:
[pre]
Single quotes: &frog - &show
Double quotes: Kermit - Sesame Street
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

Obs Name Sex Age Height Weight

1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0

[/pre]

Note how the quotes are "inside" the SAS program and not in the %LET. I rarely use quotes in my %LET statements.

The trick to knowing when to use quotes with macro variables and macro programs is to start with a working SAS program. for example, these Proc Prints:
[pre]
proc print data=sashelp.class;
title "Obs for Mary";
title2 "working SAS Program";
where name = "Mary";
run;

proc print data=sashelp.class;
title "Obs for Age=15";
where age = 15;
run;
[/pre]

Both work, they are syntactically correct. (I'm not going to show the output from these steps because you can duplicate this code and run the programs yourself to see the results. What you should notice is that in the first PROC PRINT, my where clause is for NAME, which is a character variable -- so I need to have quotes around the string "Mary" for the WHERE statement to be syntactically correct.

In the second PROC PRINT, I am building a WHERE statement for a numeric variable, AGE. So for that statement to be syntactically correct, I do NOT have quotes around the AGE value in the WHERE statement.

SAS treats all macro variables as character strings unless you use a statement that does an implicit conversion to numeric or do an explicit conversion using %EVAL. But this is an OK thing. In this statement:
[pre]
WHERE age = 15;
[/pre]
there's no difference between the macro facility typing 15 for me or me typing it myself. As long as the 15 is a number, without quotes by the time it gets to the compiler, it will be treated OK.

So now, let's say I have these 2 macro variables:
[pre]
%let want = Mary;
%let age = 15;
[/pre]

and I want to use those macro variables in versions of the above program. Well, I know from my test that the quotes are where they need to be and that the above code works to generate the desired results. So all I have to do is put the Macro variables where I have my hard-coded values in the program. Here's the converted Proc PRINT steps:
[pre]

proc print data=sashelp.class;
title "Obs for &want";
where name = "&want";
run;


proc print data=sashelp.class;
title "Obs for Age=&age";
where age = &age;
run;
[/pre]

I do need to have quotes around the title string text. So we know from the first example that those quotes need to be double quotes because macro variables do not resolve in single quotes.

The "macro-ized" version of the program steps will give the same results as the original program steps.

So, in order to use macro variables correctly and to know when to quote or not, it's easier to figure out, if you start with a working SAS program. I have always found this to be a good introduction to the Macro facility and how to construct programs and use macro variables:
http://www2.sas.com/proceedings/sugi28/056-28.pdf

cynthia
N/A
Posts: 0

Re: macro- %let statements

If you are wanting to use a macro, you don't necessarily need to use %let statements.

You can parameterize the macro:
[pre]
%macro create_chart(midpoints, outpath);
....
filename outfile "&outpath";
goptions gsfname=outfile;
...
proc gchart ... ;
vbar ... / midpoints=(&midpoints) ... ;
...
%mend;
[/pre]
usage is could then be
[pre]
%create_chart(5 to 100 by 5 , C:\desktop\SAS\graphs );
[/pre]

To really know what you are doing with SAS for this stuff, you need to read the SAS documentation which is available through the SAS Website. The complete doucmentation for SAS 9.1.x is available as HTML and can be easily browsed and used. BUT, finding it is a chore, yet the experience of finding it is useful.
Ask a Question
Discussion stats
  • 3 replies
  • 214 views
  • 0 likes
  • 3 in conversation