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


%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


1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

12 REPLIES 12
Astounding
PROC Star

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.

robertrao
Quartz | Level 8

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

Astounding
PROC Star

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.

robertrao
Quartz | Level 8

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

Astounding
PROC Star

No problem.  Hard to get angry for doing something that we all have done at one point or another.  Smiley Happy

Even harder to get angry when you solved the problem and we don't have to do anything more.  :smileylaugh:

Good luck.

robertrao
Quartz | Level 8


Also the code doenot give me any output.neither any error after i run it

Thanks

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
robertrao
Quartz | Level 8

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=" ";

Astounding
PROC Star

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.

robertrao
Quartz | Level 8

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

Astounding
PROC Star

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.

robertrao
Quartz | Level 8


Thanks so much..

Great help. I shall try it myself too...

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 12 replies
  • 1248 views
  • 3 likes
  • 3 in conversation