Hi Guys, Can you please help me to write SAS macro parameter equivalent code in Python

%macro test_macro(date, metnam, measuresuf, measurebase, bas_flag, month, year);
proc sql;
%local_connect_to_teradtaa(database = dbc);
create table work.tmp_mta_prodmon_ubs as
select * from connection to tera
(select
&metnam as metric_name,
extract(year from load_date)*100+ extract(month from load_date) as data_dt,
0 as mth_nbr,
sum(&measuresuf) as metric_value,
sum(&measurebase) as metric_base,
case when brand = ‘’ then ‘unknown’ else brand end as brand,
&bas_flag. as bas_calc_flag,
‘ABC’ as legal_entity
from bac.test_table
where extract(month from load_date) = &month
and extract(year from load_date) = &year
group by metric_name,data_dt,mth_nbr,brand,bas_calc_flag,legal_entity
);
quit;
%mend;

%test_macro(&dt,‘metric_a’, test_stock,test_inact,1,&mth,&yr);

Hi Guys I am trying to automate one of my sas code in python by writing equivalent code.

I really need your help to automate above code in python.

Hi Manohar, and welcome!

You asked for help:

“I really need your help to automate attached image code in python.”

Why post an image of code? Code is text. Unless you edit your code in
Photoshop, how is an image of code helpful?

Posting a screen shot is no use to people reading this by email. This is
what I see in your message:

upload://r9A7PZYa5P1Rj3sKinjAJL0Dr1x.jpeg

It actively discriminates against the blind and visually impaired who
are reading with screen reader software. And anyone who wants to work
with your code has to retype it by hand, as they cannot copy and paste
code out of a JPEG. (When I translate from one language to another, I
find that it is often easier to edit the original than to rewrite it
from scratch.)

When asking for free technical support, the people you ask to help you
out of kindness shouldn’t have to struggle to access your code. Please
post it as text. If it is short, you can include it directly in your
message, formatted correctly as code. If it is longer, please post it in
a pastebin, or give a URL to the original source.

Thank you.

Apology, here is the code

%macro test_macro(date, metnam, measuresuf, measurebase, bas_flag, month, year);
proc sql;
%local_connect_to_teradtaa(database = dbc);
create table work.tmp_mta_prodmon_ubs as
select * from connection to tera
(select
&metnam as metric_name,
extract(year from load_date)*100+ extract(month from load_date) as data_dt,
0 as mth_nbr,
sum(&measuresuf) as metric_value,
sum(&measurebase) as metric_base,
case when brand = ‘’ then ‘unknown’ else brand end as brand,
&bas_flag. as bas_calc_flag,
‘ABC’ as legal_entity
from bac.test_table
where extract(month from load_date) = &month
and extract(year from load_date) = &year
group by metric_name,data_dt,mth_nbr,brand,bas_calc_flag,legal_entity
);
quit;
%mend;

%test_macro(&dt,‘metric_a’, test_stock,test_inact,1,&mth,&yr);

What efforts have you made so far in converting the SAS macro to Python? In order to provide any meaningful help (short of writing the code for you, which we won’t do), we have to know what you’ve tried already.

If you are looking to get the results of a sql query in python, then I’d recommend pandas.read_sql as a good place to start. Take a look at the pandas user guide here: https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#sql-queries
Note that there is the params argument for passing parameters to a query, which should be used over manually crafting the query as a string where possible, to avoid SQL injection.

I am not asking you to write any code for me or not looking for any favor.

I am new to python and trying my best to automate one of my sas code in python by writing equivalent code and already automated few of my code and written equivalent code for datasteps or sql in python but in case of macro I am trying to use def function by using parameters and its arguments but whenever I am trying it’s not working but no issue thank you

Thank you :slight_smile:

Thanks for clarifying.

Could you show the Python function that you attempted to make and the error displayed? That way, we can see the part that was incorrect and help you work towards a working version.