楼主: dxystata
3531 7

如何将变量长度改为变量取值的最大长度 [推广有奖]

版主

已卖:302份资源

大师

37%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
183395 个
通用积分
15333.1475
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
298627 点
帖子
5586
精华
1
在线时间
13632 小时
注册时间
2006-6-21
最后登录
2025-12-22

初级学术勋章 初级热心勋章 中级热心勋章 初级信用勋章

楼主
dxystata 发表于 2014-10-30 20:38:13 |AI写论文
100论坛币
比如附件SAS数据集yyy: 字符变量x取值的最大长度为9,字符变量y取值的最大长度为18。就是将变量x的长度改为9,变量y的长度改为18。

数据集中变量很多,全为字符型变量,如何批量实现?谢谢!


yyy.rar (560 Bytes) 本附件包括:
  • yyy.sas7bdat

关键词:sas数据集 字符型变量 字符变量 数据集 字符型 如何

本帖被以下文库推荐

沙发
yongyitian 发表于 2014-10-30 21:56:10
  1. data xxx;
  2. length x y $25.;
  3.    do i = 1 to 20;
  4.       j = ceil(ranuni(12345)*20);
  5.       k = ceil(ranuni(1234)*22);
  6.     x = repeat('a', j);
  7.     y = repeat('b', k);
  8.     output;
  9.     end; drop i j k;
  10. run;
  11. proc contents data=xxx; run;

  12. proc sql;
  13.     select max(length(trim(x))), max(length(trim(y))) into :maxLX, : maxLY
  14.     from xxx;
  15. quit;
  16. %put maxLX=&maxlx;
  17. %put mzxLY=&maxLy;

  18. proc sql;
  19.    alter table xxx modify x char(&maxlx);
  20.    alter table xxx modify x char(&maxly);
  21. quit;
  22. proc contents data=xxx; run;
复制代码

藤椅
sniperhgy 发表于 2014-10-30 22:01:19
yongyitian 发表于 2014-10-30 21:56
这位坛友,22行有一点小bug……。

板凳
yongyitian 发表于 2014-10-30 22:18:14
sniperhgy 发表于 2014-10-30 22:01
这位坛友,22行有一点小bug……。
Yes, the above result is not correct. Here is another way.
The first variable in the cats() is a dollar sign.
  1. proc sql;
  2.     select cats('$',max(length(trim(x)))), cats('$', max(length(trim(y)))) into :maxLX, : maxLY
  3.         from xxx;
  4. quit;
  5. data zzz;
  6. length x &maxlx y &maxly;
  7.    set xxx (rename=(x=x_ y=y_));
  8.    x = x_;
  9.    y = y_;
  10.    keep x y;
  11. run;
  12. proc contents data=zzz; run;
复制代码
已有 2 人评分论坛币 学术水平 热心指数 收起 理由
eijuhz + 3 精彩帖子
crystal8832 + 36 + 2 + 2 鼓励积极发帖讨论

总评分: 论坛币 + 36  学术水平 + 5  热心指数 + 2   查看全部评分

报纸
pobel 在职认证  发表于 2014-10-31 08:20:41
下面的代码没有用楼主的数据集测试:中间的“[        DISCUZ_CODE_3        ]amp;” 是“$&”.

  1. %let libname=WORK;
  2. %let dsname=yyy;
  3. data _null_;
  4.     set sashelp.vcolumn end=last;
  5.         where libname=upcase("&libname") and upcase(memname)=upcase("&dsname");

  6.         ** codes;
  7.     length b_into a_into setlength  $1000;
  8.         retain b_into a_into setlength;
  9.         b_into=catx(",", b_into, "cats(max(length("||strip(name)||"))) length=4");
  10.         a_into=catx(",", a_into, " :"||strip(name));
  11.     setlength=catx(" ",setlength,name,'[        DISCUZ_CODE_3        ]amp;'||strip(name));

  12.         ** Call to execute the code;
  13.         if _n_=1 then
  14.       call execute("Proc sql noprint; select distinct ");
  15.         if last then do;
  16.       call execute(strip(b_into)||" into "||strip(a_into)
  17.                   ||" from "||strip(libname)||"."||strip(memname)
  18.                    ||";quit;");
  19.           call symputx('setlength', setlength);
  20.         end;
  21. run;

  22. data &libname..&dsname;
  23.     length &setlength;
  24.      set &libname..&dsname;
  25. run;
复制代码


地板
farmman60 发表于 2014-10-31 09:51:51
  1. data _null_;
  2.   set yyy end=last;
  3.   array char_vars _character_;
  4.   array max_len[1000] _temporary_;
  5.   length string $10000;
  6.   do i=1 to dim(char_vars);
  7.      max_len(i)=max(length(char_vars(i)),max_len(i));
  8.   end;
  9.   if last then do;
  10.      do i=1 to dim(char_vars);
  11.       string=catx(' ', string,vname(char_vars(i)), catt(',max _len(i)));
  12.    end;
  13.   call symput('vars',string);
  14. run;

  15. data want;
  16. length &vars;
  17. set yyy;
  18. run;
复制代码

7
farmman60 发表于 2014-10-31 10:06:50
farmman60 发表于 2014-10-31 09:51
data _null_;
  set yyy end=last;
  array char_vars _character_;
  array max_len[1000] _temporary_;
  length string $10000;
  do i=1 to dim(char_vars);
     max_len(i)=max(length(char_vars(i)),max_len(i));
  end;
  if last then do;
     do i=1 to dim(char_vars);
            string=catx(' ', string,vname(char_vars(i)),catt('$',max_len(i)));
         end;
   end;
  call symput('vars',string);
run;

data want;
length &vars;
set yyy;
run;

8
Cecilia_Xi 在职认证  发表于 2019-5-20 19:10:04
%macro change(dsn);                                         
data _null_;                                               
  set &dsn;                                                
  array qqq(*) _character_;                                
  call symput('siz',put(dim(qqq),5.-L));                  
  stop;                                                   
run;                                                        
data _null_;                                               
  set &dsn end=done;                                       
  array qqq(&siz) _character_;                             
  array www(&siz.);                                       
  if _n_=1 then do i= 1 to dim(www);                       
    www(i)=0;                                             
  end;                                                     
  do i = 1 to &siz.;                                       
    www(i)=max(www(i),length(qqq(i)));                     
  end;                                                     
  retain _all_;                                            
  if done then do;                                         
    do i = 1 to &siz.;                                    
      length vvv $50;                                      
      vvv=catx(' ','length',vname(qqq(i)),'$',www(i),';');
      fff=catx(' ','format ',vname(qqq(i))||' '||         
          compress('$'||put(www(i),3.)||'.;'),' ');        
      call symput('lll'||put(i,3.-L),vvv) ;               
      call symput('fff'||put(i,3.-L),fff) ;               
    end;                                                   
  end;                                                     
run;                                                        
data &dsn._;                                               
  %do i = 1 %to &siz.;                                    
    &&lll&i                                                
    &&fff&i                                                
  %end;                                                   
  set &dsn;                                                
run;                                                        
%mend;                                                      
%change(work.kyle);               

摘自:http://support.sas.com/kb/35/230.html
                  

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-8 10:32