简体   繁体   English

SAS 数组循环(做while循环)

[英]SAS Array Looping (Do while loop)

data JE.KeywordMatchTemp1;
  if _n_ = 1 then do;
    do i = 1 by 1 until (eof);
      set JE.KeyWords end=eof;
      array keywords[100] $30 _temporary_;
      keywords[i] = Key_Words;
    end;
    last_i = i ;
    retain last_i ;
  end;
  set JE.JEMasterTemp;
  match = 0;
  do i = 1 to last_i while (match=0) ;
    if index(descr, trim(keywords[i]) ) then match = 1;
  end;
  drop i last_i;
run;

Hi Can I ask what does this statement does if _n_ = 1 then do;嗨,请问if _n_ = 1 then do;

I have tried deleting this statement and the set statement set JE.JEMasterTemp;我试过删除这个语句和 set 语句set JE.JEMasterTemp; only reading in 1 observation.仅读取 1 次观察。

I need help...我需要帮助...

The _ N _ Variable represents the number of times the DATA step has iterated. _ N _ 变量表示 DATA 步迭代的次数。 You can read more about it here: Automatic Variables .您可以在此处阅读更多相关信息: 自动变量 So, on the first iteration only, you:因此,仅在第一次迭代中,您:

  • Read the JE.Keywords data set one obs at the time and increments the variable i for each obs.每次读取一个 obs 的 JE.Keywords 数据集,并为每个 obs 递增变量 i。

  • inserts the Key_Words value into the i'th place in the keywords array.将 Key_Words 值插入到关键字数组的第 i 个位置。

  • Assigns the value of i to the variable last_i when the entire JE_Keywords has been read.当整个 JE_Keywords 已被读取时,将 i 的值分配给变量 last_i。

One thing to remember is that most data steps do not stop at the end of the data step.要记住的一件事是,大多数数据步骤不会在数据步骤结束时停止。 Instead they stop when they read past the end of the input (either in INPUT statement or a SET/MERGE statement).相反,当他们读到输入结束时(在 INPUT 语句或 SET/MERGE 语句中),它们会停止。

The _N_ automatic variable is incremented once per data step iteration. _N_自动变量在每个数据步迭代中递增一次。 So _N_=1 is a test for whether this is the first iteration.所以_N_=1是测试这是否是第一次迭代。 The data step is designed to read ALL of KEYWORDS on the first iteration.数据步骤旨在在第一次迭代时读取所有关键字。 And then to read one observations from JEMASTERTEMP per iteration.然后在每次迭代中从 JEMASTETEMP 中读取一个观察结果。 So normally the data step stops when it reads past the end of JEMASTERTEMP.因此,通常数据步在读取超过 JEMASTETEMP 末尾时停止。

By removing that line what happens on the second iteration is that it will try to read more observations from KEYWORDS, but it read all of the observations on the first iteration.通过删除该行,在第二次迭代中发生的事情是它会尝试从 KEYWORDS 中读取更多观察结果,但它会在第一次迭代中读取所有观察结果。 So without that IF statement the step stops at the top of the second iteration, so only one observation is written.因此,如果没有该 IF 语句,该步骤将在第二次迭代的顶部停止,因此只写入一个观察值。

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

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