如何在SAS中带有字符串前缀的数字宏变量

发布于 2025-02-03 01:37:59 字数 643 浏览 2 评论 0原文

我在召回数字宏变量的数据集中创建新字段时在SAS中获得错误。这里有一个例子。

data input;
input cutoff_1 cutoff_2;
datalines;
30 50
;

data db;
input outstanding;
datalines;
1000.34
2000.45
3000.90
5000.98
8000.02
;


data _null_;
set input;
call symput("perc1",cutoff_1);
call symput("perc2",cutoff_2);
run;
    
proc univariate data=db noprint;
    var outstanding;
 
    output out=test
    pctlpts = &perc1. &perc2.
    pctlpre = P_;
run;

data test2;
set test;
p_&perc1._round=round(P_&perc1.,1);
p_&perc2._round=round(P_&perc2.,1);
run;

从日志中,似乎已经解决了宏& perc。在数据集test2中命名一个新变量。我想念什么?

I obtain an error in SAS while creating a new field in a dataset recalling a numeric macro variable. Here there's an example.

data input;
input cutoff_1 cutoff_2;
datalines;
30 50
;

data db;
input outstanding;
datalines;
1000.34
2000.45
3000.90
5000.98
8000.02
;


data _null_;
set input;
call symput("perc1",cutoff_1);
call symput("perc2",cutoff_2);
run;
    
proc univariate data=db noprint;
    var outstanding;
 
    output out=test
    pctlpts = &perc1. &perc2.
    pctlpre = P_;
run;

data test2;
set test;
p_&perc1._round=round(P_&perc1.,1);
p_&perc2._round=round(P_&perc2.,1);
run;

From the log it seems that the macros &perc. are solved, but that it's not possible to use those results (30, 50) to name a new variable in the dataset test2. What am I missing?

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

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

发布评论

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

评论(2

酒儿 2025-02-10 01:37:59

当您要求普通的SAS代码在需要字符值的地方使用数值(两个要调用Symput()需要字符值的参数)时,SAS将使用BEST12将数字转换为字符串。如果值不需要所有12个字符,则将对齐。因此,您创建了带有领先空间的宏变量。引导空间对于生成pctlpts =选项值没有区别,因为额外的空格在那里无关紧要。但是拥有领先空间意味着您正在生成类似的代码:

p_         30_round=round(P_          30,1);

您应该使用现代(可能超过20年的历史call symputx()函数。创建宏变量时,该功能将从第二个参数中删除前导/尾随空间。它也接受数字值,因为第二个参数将数字转换为字符串,更类似于Best32而不是Best12。

call symputx("perc1",cutoff_1);
call symputx("perc2",cutoff_2);

唯一应该使用古代呼叫Symput()函数的情况是,您实际需要创建包含前导和/或尾随空间的宏变量。

其他解决方案是删除函数调用中的空间

call symput("perc1",strip(put(cutoff_1,best32.)));

或生成宏变量后。

%let perc1=&perc1;

When you ask normal SAS code to use a numeric value in a place where a character value is required (both arguments to CALL SYMPUT() require character values) then SAS will convert the number into a string using the BEST12. If the value does not require all 12 characters it will be right aligned. So you created macro variables with leading spaces. The leading spaces make no difference for generating pctlpts= option values as extra spaces will not matter there. But having the leading spaces mean you are generating code like:

p_         30_round=round(P_          30,1);

You should use the modern (probably over 20 years old) CALL SYMPUTX() function instead. That function will remove leading/trailing spaces from the second argument when creating the macro variable. It also accepts a numeric value as the second argument converting the number into a character string using a format more like BEST32 instead of BEST12.

call symputx("perc1",cutoff_1);
call symputx("perc2",cutoff_2);

The only cases where you should ever use the ancient CALL SYMPUT() function is when you actually need to create macro variables that contain leading and/or trailing spaces.

Other solutions are to remove the spaces in the function call

call symput("perc1",strip(put(cutoff_1,best32.)));

or after generating the macro variables.

%let perc1=&perc1;
下壹個目標 2025-02-10 01:37:59

一种可能的解决方案是使用调用Symputx而不是调用Symput。使用调用Symputx创建的变量是全局定义的。

One possible solution is to use call symputx and not call symput. With call symputx the variables created are globally defined.

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