我试图用 x 中第一个观察值(即 1)替换 r<=10 的所有 r 值。这只是我想要做的事情的一个非常简单的例子,所以请不要质疑为什么我试图以复杂的方式来做这件事,因为完整的代码更复杂。我唯一需要帮助的是弄清楚如何使用我创建的向量 (p1) 来替换 r[p1] 或等效的 r[c(1,2,3,4)] 与 x[ 1 ] (等于至 1)。我无法显式编写 p1,因为它将在循环中生成(代码中未显示)。
x=c(1,2,3)
r=c(1,3,7,10,15)
assign(paste0("p", x[1]), which(r<=10))
p1
r[paste0("p", x[1])]=x[1]
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我尝试使用,r[paste0("p", x[1])]=x[1] 但这是我最终得到的输出
基本上,我需要找出一种在这段代码中调用 p1 的方法,r[??]=x[1] 而无需显式键入 p1。
我已经包含了我在下面尝试的完整代码,以防需要上下文。
##Creates a function to generate discrete random values from a specified pmf
##n is the number of random values you wish to generate
##x is a vector of discrete values (e.g. c(1,2,3))
##pmf is the associated pmf for the discrete …Run Code Online (Sandbox Code Playgroud) 当我运行宏"quantplot"时,我遇到了图片中显示的问题:
图片错误.
基本上似乎正在发生的事情是,这种形式的表达式recode_&variable,out_和variables等都没有返回正确的值.它只是将其读作recode_并忽略宏变量.
我为示例数据提供了以下代码:
data test (drop=i);
do i=1 to 1000;
a=round(uniform(1)*4,.01);
b=round(uniform(1)*10,.01);
c=round(uniform(1)*7.5,.01);
u = rand("Uniform");
y=floor( (2)*u );
output;
end;
stop;
run;
%macro percentiles(indep_var=,num_groups=);
%let recodes=recode_%sysfunc(tranwrd(&indep_var,%str( ),%str( recode_)));
proc rank data=test out=test groups=7;
var &indep_var;
ranks &recodes;
run;
%mend;
%percentiles(indep_var=a b c);
Run Code Online (Sandbox Code Playgroud)
还有目前无效的宏代码.请帮忙!
/*Plots to determine functional form for quantitative variables*/
%macro quantplot(indep_var=,dep_var=);
/* Count the number of words in the input */
%let count=%sysfunc(countw(&indep_var));
/* Loop through the total number of values */
%do i = …Run Code Online (Sandbox Code Playgroud)