%macro freqs(want=,var=,have=,label=);
data &want(RENAME=(&var=PARAM));
length label$150 &var$100 count percent 8.;
set $have;
if _n_= 1 then
label= "&label" ;else label=" ";
Total=0+count;
run; /*if quotes necesarry while referencing labels ie label="AGE"*/
%freqs(want=age_1,var=Age,have=Age_freqs,label=AGE);
Hi in the macro above can the variable name be similar to the macro reference?
i mean
label= "&label" ;else label=" ";
Also should the AGE be in quotes when referencing????????since its a lablel
karun,
You might be better-served by using just a single macro variable:
%macro freqs (var=);
data &var._1 (rename=(&var=PARAM));
length ...;
set have;
if _n_=1 then label="%upcase(&var)"; else label=" ";
total = count;
run;
%mend freqs;
%freqs (var=Age)
It would become important to use initial caps (Age) when calling the macro. I would guess this makes the macro much easier to read and maintain.
The question about quotes makes sense in context only. What should the generated SAS code look like? If the SAS program requires quotes, then use quotes. Macro language isn't relevant to the question, except that macro language must generate the accurate SAS program.
Good luck.
karun,
You might be better-served by using just a single macro variable:
%macro freqs (var=);
data &var._1 (rename=(&var=PARAM));
length ...;
set have;
if _n_=1 then label="%upcase(&var)"; else label=" ";
total = count;
run;
%mend freqs;
%freqs (var=Age)
It would become important to use initial caps (Age) when calling the macro. I would guess this makes the macro much easier to read and maintain.
The question about quotes makes sense in context only. What should the generated SAS code look like? If the SAS program requires quotes, then use quotes. Macro language isn't relevant to the question, except that macro language must generate the accurate SAS program.
Good luck.
Hi Astounding,
Thanks for the response and sorry for the delay.
each time the want and have changes and so i included them in the referencing.
Also my second question was if i can have
label=&label (variable name is same like the refencing variable)..........should it be different??????
&label being referenced to may be apple.........
Thanks
karun,
It is perfectly fine to give LABEL a value of the name of a variable. I'm guessing here, but I would imagine you intend to use LABEL later, as part of an output data set from PROC FREQ. There might be other ways to extend your macro, and have macro language pick out that information instead of creating LABEL. But your approach would work (and there's a lot to be said for any program that works!).
As was stated, if you are not getting output you would have to show us a little more for us to figure out what is happening. Here is one possibility that would give you no output at all. Suppose you defined your macro with default parameters, so the macro could be called using default values for all parameters. In that case, this is not enough to cause the macro to start executing:
%freqs
In an interactive environment, SAS wouldn't know that your call to the macro was complete. You would at least have to add:
%freqs ()
That way, SAS knows you don't intend to overwrite any of the default parameters. Of course, there are a lot of other possibilities so show us the code and we'll figure it out.
Good luck.
Thanks so much for your detailed explanation of the topic.
I figured out I was running the program without the %mend;
Dont get angry on me...
Thanks
No problem. Hard to get angry for doing something that we all have done at one point or another.
Even harder to get angry when you solved the problem and we don't have to do anything more. :smileylaugh:
Good luck.
Also the code doenot give me any output.neither any error after i run it
Thanks
karun wrote:
Also the code doenot give me any output.neither any error after i run it
We need to see your code, and the related parts of the SASLOG.
Astounding,
In the code below %upcase and then the macrovariable in braces would give uppercase of the var???????
If so What are the other options helpful like this one???
Thanks
if _n_=1 then label="%upcase(&var)"; else label=" ";
This expression:
label="%upcase(&var)";
is giving the uppercase of the value assigned to the macro variable &VAR. There are very, very few additional options as macro language does not contain many functions. However, two items are worthy of note.
1. The DATA step contains many functions. This would be a reasonable alternative:
label = upcase("&var");
The DATA step contains hundreds of functions (you'll have to do your own work to look them up). In some cases, however, using a DATA step function to create a new variable assigns a length to the new variable that is longer than what you would assume. You'll have to look at the function documentation and test it.
2. Macro language allows access to most all DATA step functions, but the programming gets a little involved to make it happen. The macro function %SYSFUNC is used to access functions that are not built into macro language including (most) DATA step functions. Too many details to go into here, since it's not a topic that you'll need yet.
Good luck.
Hi Astounding,
Do u mean both the steps below gives the same result????????irrespective of the position of the braces????
label="%upcase(&var)";
label = upcase("&var");
Thanks
Yes, the results are identical.
Try it for yourself. Use two variables, LABEL1 and LABEL2. Then run a PROC PRINT and a PROC CONTENTS. For the first statement, you have to picture how SAS would handle this statement:
label="AGE";
That's what SAS sees, after macro language gets done interpreting %upcase(&var).
Good luck.
Thanks so much..
Great help. I shall try it myself too...
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.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: