如何在SAS中带有字符串前缀的数字宏变量
我在召回数字宏变量的数据集中创建新字段时在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您要求普通的SAS代码在需要字符值的地方使用数值(两个要调用Symput()需要字符值的参数)时,SAS将使用BEST12将数字转换为字符串。如果值不需要所有12个字符,则将对齐。因此,您创建了带有领先空间的宏变量。引导空间对于生成pctlpts =选项值没有区别,因为额外的空格在那里无关紧要。但是拥有领先空间意味着您正在生成类似的代码:
您应该使用现代(可能超过20年的历史)
call symputx()
函数。创建宏变量时,该功能将从第二个参数中删除前导/尾随空间。它也接受数字值,因为第二个参数将数字转换为字符串,更类似于Best32而不是Best12。唯一应该使用古代呼叫Symput()函数的情况是,您实际需要创建包含前导和/或尾随空间的宏变量。
其他解决方案是删除函数调用中的空间
或生成宏变量后。
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:
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.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
or after generating the macro variables.
一种可能的解决方案是使用
调用Symputx
而不是调用Symput
。使用调用Symputx
创建的变量是全局定义的。One possible solution is to use
call symputx
and notcall symput
. Withcall symputx
the variables created are globally defined.