【问题标题】:let function and array statement in a macro宏中的 let 函数和数组语句
【发布时间】:2018-02-27 06:12:05
【问题描述】:

我是 SAS 和宏的新手。我需要一些帮助来理解以下代码行的作用:

%let numbercccats=201;

DATA &OUTDSN(KEEP=HICno CASE &prefix.CC1-&prefix.CC&numbercccats. ID) ERR;
SET TEMP1;
by ID;
length cc $4.;

cc=left(addxg);

RETAIN &prefix.CC1-&prefix.CC&numbercccats. 0 ; 
ARRAY C(&numbercccats.)  &prefix.CC1-&prefix.CC&numbercccats.;

谢谢。

【问题讨论】:

  • 也许你可以添加关于你使用什么语言的标签?

标签: arrays macros sas


【解决方案1】:

我在下面的代码中添加了我对 SAS 代码的每一行的解释:

%let numbercccats=201;
/*Creates macro variable numbercccats with value 201*/

DATA &OUTDSN
/*Creates a SAS table; the table name will be te value in the macro variable OUTDSN*/

(KEEP=HICno CASE &prefix.CC1-&prefix.CC&numbercccats. ID) ERR;
/*Keep statement only keeps the column specified above*/
/*&prefix.CC1-&prefix.CC&numbercccats. is resolved to the value of the macro prefix concatinated with CC1-CC201*/
/*in other words you bring/keep 200 columns CC1,CC2,...CC201*/

SET TEMP1;
/*The input/source table is TEMP1 */

by ID;
/*Group by ID*/

length cc $4.;
/*Creates a new column named CC of length 4*/

cc=left(addxg);
/*Assigns the value of addxg to the new column cc, and removes leading spaces */

RETAIN &prefix.CC1-&prefix.CC&numbercccats. 0 ; 
/*for all the records with the same ID SAS won't change the value of the 200 columns CC1 to CC201*/

ARRAY C(&numbercccats.)  &prefix.CC1-&prefix.CC&numbercccats.;
/*An array called C of length 201 is created pointing to the 201 columns: CC1-CC201 */

【讨论】:

  • 设置温度1; /*创建的新表名为 TEMP1*/ ???????使用“set”,表读取未创建。
  • 谢谢@momo1644。只是为了澄清这个声明 - ARRAY C(&numbercccats.) &prefix.CC1-&prefix.CC&numbercccats.;哪个是指针数组?是 C(&numbercccats.) 吗?我不确定这是否是声明 2 个数组或其他内容的声明。你说它只是一个指针数组声明?如果我在理解这行代码时出现错误,请纠正我。
  • 数组名是C,分界线翻译为C(201) CC1-CC201。因此,如果您遍历 201 个数组对象,您将遍历 201 个列值。
  • 这个简短的 SAS 阵列教程将让您很好地了解它的工作原理:stattutorials.com/SAS/Tutorial-SAS-ARRAYS.html
  • 感谢您的确认。我不确定为什么要这样做,因为我认为 CC(20) 会将您带到第 20 个索引。但是,您提到的“列”基本上应该是 CC(1,20)。当我试图更多地理解数组时,将通过本教程。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 2017-10-14
  • 2019-04-16
  • 1970-01-01
  • 2021-03-27
相关资源
最近更新 更多