如何在python中编写SAS宏参数的等效代码

2024-05-29 11:59:00 发布

您现在位置:Python中文网/ 问答频道 /正文

需要使用def函数在python中创建和编写SAS宏参数的等效代码。 如果是,请帮助我,我正在尝试用python自动执行下面的代码

抱歉,我不知道如何在这里插入excel文件数据, 但在enter image description here下面,我插入了数据图像,它只有20行数据,供您参考。

/******转换Pyspark或python中的SAS宏参数*****/

/创建表test1及其变量/ /度量单位名称、性别、度量单位值、度量单位基数、基本计算标志、法律实体/ /创建了空结构表test_data_3,基于我可以在最终输出下面插入的位置/

proc sql;
create table test_1 as
select 'age_male' as metric_name ,
sex,
sum(Height) as metric_value,
sum(Weight) as metric_base,
1 as bas_calc_flag,
'ABC' as legal_entity
from sashelp.class
group by 1,2,5,6
order by 1,2,5,6; quit;
 
 data test_1;
 set test_1 ;
 drop sex; run;

 proc sql;
 create table test_2 like test_1;
 quit;

 %macro test_macro(metnam,sex, measuresuf, measurebase, bas_flag);
proc sql;
create table test_3 as
select 
&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
 where sex =  &sex
 group by 1,4,5
 order by 1,4,5; quit;
 
 proc sql;
 create table test_4 as
 select * from test_3; quit; 

 /**inserting final output in below empty table */
 proc sql;
 insert into  test_2 
 select * from  test_4; quit;

 %mend;
 %test_macro('H_Feml','F',Height ,Weight, 1);
 %test_macro('H_male','M',Height ,Weight, 1);
 %test_macro('age_male','M',Age ,Weight, 1);
 %test_macro('age_Feml','F',Age ,Weight, 1);

 proc sql;
 select * from test_2; quit; 
 ****here is output image[enter image description here][2]****

Tags: fromtestsqlascreatetableprocmetric

热门问题