将SAS转换为Python

发布于 2025-01-27 23:08:02 字数 825 浏览 1 评论 0原文

我正在将SAS代码程序转换为python等效。我正在努力的一个部分是如何在使用宏中使用的变量来创建数据集时转换SAS中的宏程序。例如:

%macro program(type);
data portfolio_&type.;
set portfolio;
run;

我基本上想创建一个portfolio_ type的数据框架。知道我如何继续这样做吗?

Edit: I don't think I have enough detail originally

Say my data has a column called type, and that takes either value of 'tree' or 'bush', I want to split my data in two, and then process the same functions在这两个上都为两者创建单独的输出表。在SAS中,这很简单。我编写了有效的功能,可以将我的参数置于代码中,从而使它们成为独特的数据集。

%macro program(type);
data portfolio_&type.;
set portfolio (where=(type=&type.));
run;


Proc freq data=Portfolio_&type.;
Tables var1/out=summary_&type.;
Run;
%mend;
%program(Tree);
%program(bush);

&允许我将文本放入数据集名称中,但是我无法使用python中的DEF函数类型语句来完成此操作,因为我无法将参数放入我的数据帧名称中

I'm converting a program of SAS code into a python equivalent. One section that i'm struggling with is how to convert a macro program in SAS when the variables used within the macro are used to create a dataset. for example:

%macro program(type);
data portfolio_&type.;
set portfolio;
run;

I basically want to create a dataframe equivalent of portfolio_&type. Any idea how I proceed with this?

Edit: I don’t think I have enough detail originally

Say my data has a column called type, and that takes either value of ‘tree’ or ‘bush’, I want to split my data in two, and then process the same functions on both and create separate output tables for both. In SAS this is quite simple. I write macros which are effectively functions that take my arguments and drop them into the code, making them unique datasets.

%macro program(type);
data portfolio_&type.;
set portfolio (where=(type=&type.));
run;


Proc freq data=Portfolio_&type.;
Tables var1/out=summary_&type.;
Run;
%mend;
%program(Tree);
%program(bush);

The & allows me to drop my text into the dataset name but I can’t do this with a def function type statement in python because I can’t drop the argument into my data frame name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2025-02-03 23:08:02

您的SAS Macro希望动态命名为Proc Freq的SAS数据集。 Pandas DataFrames不能动态命名。您需要将已解决的宏变量值portfolio_tree和portfolio_bush用作数据框架名称。 SAS数据集名称对病例不敏感,而PANDAS DataFrame对病例敏感。

对于Proc Freq,您将拥有
portfolio_tree ['var1']。value_counts

Your SAS macro wants to dynamically name the SAS dataset fed into PROC Freq. pandas dataframes cannot be named dynamically. You need to use the resolved macro variable value Portfolio_Tree and Portfolio_bush as the dataframe names. SAS dataset names are case-insensitive and pandas dataframe are case-sensitive.

For PROC FREQ you would then you would have,
Portfolio_Tree[‘var1’].value_counts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文