简体   繁体   English

在 sas 循环中循环变量的问题

[英]Trouble looping through variables in sas loop

Very simple: i'm trying to convert many character variables into numeric.很简单:我正在尝试将许多字符变量转换为数字。 The following code gives the "syntax error, expecting on of the following: a name, -, :, ;"以下代码给出了“语法错误,需要以下内容:名称、-、:、;” for the drop and rename line.对于删除和重命名行。

data ex; set ex;
    array numeric{3} var1 var2 var3;
    do i=1 to 8;
        temp = input(strip(numeric(i)),10.);
        drop numeric(i);
        rename temp = numeric(i);
    end;
run;

can you not use drop or rename statements in do loops??你不能在 do 循环中使用 drop 或 rename 语句吗??

The dataset structure has to be decided when the data step is compiled.在编译数据步骤时必须确定数据集结构。 So there is no way you could use an array reference in a rename statement.因此,您无法在重命名语句中使用数组引用。

If you really have simple numerically suffixed variable names then you could use a simple RENAME statement.如果您确实有简单的数字后缀变量名,那么您可以使用简单的 RENAME 语句。

rename new1-new3=var1-var3;

So your program might be as simple as this:所以你的程序可能像这样简单:

data want;
  set have;
  array ch var1-var3;
  array new new1-new3;
  do index=1 to dim(ch);
    new[index]=input(left(ch[index]),32.);
  end;
  drop index var1-var3;
  rename new1-new3=var1-var3;
run;

If the list of names is more complex, like AGE HEIGHT WEIGHT for example, then you will need to use a more complex RENAME statement like:如果名称列表更复杂,例如 AGE HEIGHT WEIGHT,那么您将需要使用更复杂的 RENAME 语句,例如:

rename new1=AGE new2=HEIGHT new3=WEIGHT ;

So use some type of code generation method.所以使用某种类型的代码生成方法。 Like macro code or using a data step to write lines of code to a file that can be included into the program using %include statement.像宏代码或使用数据步骤将代码行写入文件,该文件可以使用%include语句包含到程序中。

For example you could make a macro like this:例如,您可以制作这样的宏:

%macro rename(varlist);
%local i;
rename
%do i=1 %to %sysfunc(countw(&varlist));
   new&i=%scan(&varlist,&i)
%end;
;
%mend ;

And use it like this:并像这样使用它:

%let charvars=AGE HEIGHT WEIGHT;

data want;
  set have;
  array ch &charvars;
  array new [%sysfunc(countw(&charvars))];
  do index=1 to dim(ch);
    new[index]=input(left(ch[index]),32.);
  end;
  drop index &charvars;
  %rename(&charvars);
run;

You're doing a couple of things incorrectly for SAS.您为 SAS 做了一些错误的事情。

  1. Don't use the same data set name in the DATA/SET statements.不要在 DATA/SET 语句中使用相同的数据集名称。 It's bad practice and makes it much harder to debug your code.这是不好的做法,使调试代码变得更加困难。

  2. You cannot change the type to the same variable name in the same data step.您不能在同一数据步骤中将类型更改为相同的变量名称。 Often you can rename ahead of time to make this slightly easier.通常,您可以提前重命名以简化此操作。

  3. Ideally, especially if the file was read from a text file you fix these issues at the data import stage, not after the fact.理想情况下,特别是如果文件是从文本文件中读取的,您可以在数据导入阶段而不是事后解决这些问题。

I don't know if the DROP/RENAME statements will take the array variable appropriately, that's something that would need to be tested.我不知道 DROP/RENAME 语句是否会适当地采用数组变量,这是需要测试的东西。

data ex1; 
set ex;

*original character variables;
array _chars(3) var1-var3;
*new numeric variables;
array _nums{3} new_var1-new_var3;

do i=1 to 3; *should match size of array;
    _nums(i) = input(strip(_chars(i)), 10.);
end;

drop var1-var3;

*not sure if this will work in the same step;
rename new_var1-new_var3 = var1-var3;

run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM