楼主: zgwshmily
2884 5

如何解决Name is too long for a SAS name in this context [推广有奖]

  • 0关注
  • 1粉丝

大专生

28%

还不是VIP/贵宾

-

威望
0
论坛币
2129 个
通用积分
0.5400
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
287 点
帖子
25
精华
0
在线时间
20 小时
注册时间
2010-8-28
最后登录
2013-2-25

40论坛币
我有一段非常优秀的code如下, 可以实现找到每个变量的missing value的百分比,大家一运行便知结果

data survey;
  input id diet exer hours xwk educ;
  datalines;
1 1 . 1 3 1
1 . 2 1 4 2
1 . 4 . . .
1 1 5 2 3 .
2 . 9 2 3 .
2 5 9 2 4 .
2 . 3 . 5 3
3 2 . . . .
;

proc sql noprint;
    select distinct name into : var_list separated by " "
      from dictionary.columns
      where  libname="WORK" and memname="SURVEY" and upcase(name) ne "ID";
quit;
%put &var_list;

%macro count_miss(ds= , byvar= , vars= );

%let i=1;
%let var1=%sysfunc(scan(&vars,1));
%do %until(&v eq );

%let i=%eval(&i+1);

    %let v=%sysfunc(scan(&vars,&i));

%let var&i=&v;
%end;
%let varnum=%eval(&i-1);

proc sql;
    create table countmiss as
    select &byvar

%do i=1 %to &varnum;
              ,sum(missing(&&var&i)) as &&var&i.._miss

%end;

from &ds

group by &byvar;

    create table counttotal as
    select distinct &byvar, count(*) as total

from &ds

group by &byvar;


create table countmiss_pct_&ds as

select a.&byvar

%do i=1 %to &varnum;
              ,(&&var&i.._miss)/total as &&var&i.._miss_pct

%end;

from countmiss a, counttotal b

where a.&byvar=b.&byvar;
quit;

proc print data=countmiss_pct_&ds; run;
%mend;

%count_miss(ds=survey, byvar=id, vars=&var_list);




但是针对上面这个survey数据没问题,但在我实际工作中的数据却会出错。因为我的变量名字都太长,比如有个变量叫‘TOTAL_CONTROL_DOSAGE_UNITS_MISS_PCT' 在log里面就会显示下面这个样的error:

“Name 'TOTAL_CONTROL_DOSAGE_UNITS_MISS_PCT' is too long for a SAS name in this context”

因为我的变量名字都是这么长,所以想请教一下如何解决这个问题。比如把上面的data set中的变量名educ改成TOTAL_CONTROL_DOSAGE_UNITS_MISS_PCT
data survey;
  input id diet exer hours xwk TOTAL_CONTROL_DOSAGE_UNITS_MISS_PCT;
  datalines;
1 1 . 1 3 1
1 . 2 1 4 2
1 . 4 . . .
1 1 5 2 3 .
2 . 9 2 3 .
2 5 9 2 4 .
2 . 3 . 5 3
3 2 . . . .
;


在运行宏%count_miss(ds=survey, byvar=id, vars=&var_list); 就会有下面的error

ERROR: The variable named TOTAL_CONTROL_DOSAGE_UNITS_MISS_PCT contains more than 32 characters.

垦请高手如何修改我的%count_miss让程序可以解决变量名长度的问题。

最佳答案

pobel 查看完整内容

应该不是宏的问题,SAS限制变量名的长度最大为32.
关键词:Context Contex name This Text SAS Context long name name

本帖被以下文库推荐

沙发
pobel 在职认证  发表于 2010-8-31 10:38:25 |只看作者 |坛友微信交流群
应该不是宏的问题,SAS限制变量名的长度最大为32.
和谐拯救危机

使用道具

藤椅
pobel 在职认证  发表于 2010-8-31 10:51:46 |只看作者 |坛友微信交流群
SAS遇到变量名长度大于32时就会报错,好像只有修改变量名使其长度在32个字符以内了。


27   data test;
28        TOTAL_CONTROL_DOSAGE_UNITS_MISS_PCT=3;
ERROR: The variable named TOTAL_CONTROL_DOSAGE_UNITS_MISS_PCT contains more than 32 characters.
29   run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TEST may be incomplete.  When this step was stopped there were 0 observations and 0 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.03 seconds
和谐拯救危机

使用道具

板凳
woaiwojia9 发表于 2010-8-31 12:40:32 |只看作者 |坛友微信交流群
Yes. SAS variable name and dataset name have limitation.

使用道具

报纸
lyceum 发表于 2010-8-31 15:11:19 |只看作者 |坛友微信交流群
不知这个网站的办法是否有些启发
http://ciser.cornell.edu/FAQ/SAS/variable_length.shtml

使用道具

地板
soporaeternus 发表于 2010-8-31 15:26:19 |只看作者 |坛友微信交流群
为什么要用那么长的变量名?
Let them be hard, but never unjust

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 00:38