SAS 数组创建

SAS Array creation

我正在尝试创建包含一个值的数组。

proc sql noprint;
select count(*) into :dscnt from study;
select libname into :libname1 - :libname&dscnt from study;
quit;

我认为语法是正确的,但我在 SAS studio 中不断收到以下错误消息。

***NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
NOTE: Line generated by the macro variable "DSCNT".
79         libname       4
                      _
                      22
                      200
ERROR 22-322: Syntax error, expecting one of the following: ',', FROM, NOTRIM.  

ERROR 200-322: The symbol is not recognized and will be ignored.***

有人可以向我解释我做错了什么吗?

谢谢

做如下的事情

 proc sql noprint;
 select count(*) into :dscnt from sashelp.class;
  select name into :name1 - :name%left(&dscnt) from sashelp.class;
  quit;

proc sql 中的 into 语法将格式化值存储到宏变量中。例如,如果您 运行 此代码:

proc sql noprint;
  select count(*) into :dscnt from sashelp.class;
quit;

%put #&dscnt#;

你会看到输出是:

#      19#

换句话说,结果用空格填充。这意味着在您的示例中,代码正在解析为:

select libname into :libname1 - :libname      19 from study;

^ 这显然是无效语法。要解决此问题,您只需将 TRIMMED 关键字添加到 SQL 语句中即可:

select count(*) into :dscnt TRIMMED from study;

感谢 Reeza 提供 TRIMMED 关键字。

您不需要提前知道项目的数量,如果您将其留空,SAS 将自动创建正确数量的宏变量。

如果您确实想在其他地方使用该号码,您可以使用 TRIMMED 选项创建它以删除任何额外的空格。请参阅下面的第二个示例。

proc sql noprint;
select name into :name1- from sashelp.class;
quit;

%put &name1;
%put &name19.;

proc sql noprint;
select count(distinct name) into :name_count TRIMMED from sashelp.class;
quit;

%put &name_count;

结果:

3068      proc sql noprint;
3069      select name into :name1- from sashelp.class;
3070      quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


3071
3072      %put &name1;
Alfred
3073      %put &name19.;
William
3074
3075      proc sql noprint;
3076      select count(distinct name) into :name_count TRIMMED from
3076! sashelp.class;
3077      quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


3078
3079      %put &name_count;
19