BookmarkSubscribeRSS Feed
ManoharNath
Obsidian | Level 7

Hi, 

I am working on to replicate this SAS code in another way in SAS means in data step or proc sql without using any macro,  please help me. What I am doing here is that, I've  created two metrics or measures which have metric value and metric base value to reflect in my reports.

 

This codes is working but I am looking for another way and need to know can I do this in another way or not.

below is the output how it will look like, I want below output via without using macro means data step or proc sql.

ManoharNathGupta_0-1621352064716.png

 

 

%macro test_macro(sex, metnam, measuresuf, measurebase, bas_flag);
proc sql;
create table tan_1 as
select sex,&metnam as metric_name, sum(&measuresuf) as metric_value,
sum(&measurebase) as metric_base,
&bas_flag as bas_calc_flag,
'ABC' as legal_entity
from sashelp.class
group by 1
order by 1
;
quit;
%mend;
%test_macro('M','age_male',Height ,Weight, 1);
%test_macro('F','age_Feml',Height ,Weight, 1);


data tan_2;
set tan_1;
run;
proc print data = tan_2;
run;

9 REPLIES 9
PaigeMiller
Diamond | Level 26

Please explain what this macro does.

 

Please explain why you want to do it a different way. Does it not work?

 

In the future, it would be very helpful to us if you indented your code properly, this would help us decipher your code.

--
Paige Miller
CarmineVerrell
SAS Employee

Please also include sample data and example of what you want your results to look like.

Sajid01
Meteorite | Level 14

Hello @ManoharNath 
While agreeing with other that there is a need in clarity of what you want, I guest you want to handle a scanrio where one may call a macro without specifying one or more parameters. Have a look at the following

%macro greetings(mygreet="Namaskaram", Country="India");
%put &=mygreet , &=Country;
%mend greetings;
%greetings()
%greetings(mygreet="Suprabhatam")
%greetings(mygreet="Bon Jour", Country="France")

%greetings(mygreet="Hello", Country="United States")

The output will be something like as follows. If you miss to type or do not specify a value a default will be used


 
 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         %macro greetings(mygreet="Namaskaram", Country="India");
 74         %put &=mygreet , &=Country;
 75         %mend greetings;
 76         %greetings()
 MYGREET="Namaskaram" , COUNTRY="India"
 77         %greetings(mygreet="Suprabhatam")
 MYGREET="Suprabhatam" , COUNTRY="India"
 78         %greetings(mygreet="Bon Jour", Country="France")
 MYGREET="Bon Jour" , COUNTRY="France"
 79         
 80         %greetings(mygreet="Hello", Country="United States")
 MYGREET="Hello" , COUNTRY="United States"
 81         
 82         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;

As you can see the

(i) in the first macro call no parameter was specified. The defaults were taken

(ii) In the second call  the value of second parameter country was not specified , so the default values was taken for the missing.

(iii) In other macro calls ,  parameters were specified and they were taken.
If this does not answer your question please clarify what you need.

ManoharNath
Obsidian | Level 7

Thank you, but I need the output without using any macro, I have just mentioned code and shown what output is required.

ballardw
Super User

@ManoharNath wrote:

Thank you, but I need the output without using any macro, I have just mentioned code and shown what output is required.


I'm not sure I agree completely with this statement.

However your current code is likely not working because the second macro call overwrites the data set Tan_1 each time it is called, so you only get the results from the last call.

Add a parameter to name the output data set of Proc Sql and instead of

data tan_2;
set tan_1;
run;

list all the names instead just tan_1.

Hint: If you use a macro parameter to create sets with a common unique prefix you can reference them in the set statement  as:  Set prefix: ; the : list creator will use all the data sets whose names start with the given prefix text.

 

ManoharNath
Obsidian | Level 7

just used and added sashelp.class table,

Sajid01
Meteorite | Level 14

hello @ManoharNath 
In my opinion using macros is the right approach for this type of issues

KidCat
Fluorite | Level 6

Hello @ManoharNathGupta 

I do not want to judge why you do not want to use macros. There are may reasons to do, but maybe some not to. In that case, see a SAS macro as a text with "slots". Also, see a macro call as a trigger to start this macro. You have two calls, so you trigger the same "text" two times.

So, copy the "text" two times.

Replace the so-called macro variables beginning with & with the values from the first macro call in the first text

Replace the so-called macro variables beginning with & with the values from the second macro call in the second text

That should be it.

 

Sajid01
Meteorite | Level 14

Hello @ManoharNath 
From your question what I see is that 

"I am working on to replicate this SAS code in another way in SAS means in data step or proc sql without using any macro, "
Later your requirement is 

"This codes is working but I am looking for another way and need to know can I do this in another way or not.

below is the output how it will look like, I want below output via without using macro means data step or proc sql."

Can you please clarify if you want to avoid use of macros OR (Proc Means and Proc SQL) OR Both?

It you let us know why don't want to use macros' we can be on the same page and perhaps answer your question appropriately.

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
  • 9 replies
  • 1093 views
  • 0 likes
  • 6 in conversation