楼主: libravilla
1527 5

[问答] 请教用宏改变变量名 [推广有奖]

  • 0关注
  • 0粉丝

初中生

71%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
230 点
帖子
14
精华
0
在线时间
14 小时
注册时间
2009-9-24
最后登录
2019-5-7

楼主
libravilla 发表于 2013-5-10 17:27:58 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

将变原始量名改成 flt2_num_var_noms_1,flt2_num_var_noms_2,flt2_num_var_noms_3,以此类推。

码出以下程序,可是报错,不懂如何修正,请教大虾!


%macro chg_name();

%let m=0;

  %let dsid=%sysfunc(open(uat.develop_final3));

  %if &dsid gt 0 %then %do;

  %let nobs=%sysfunc(attrn(&dsid,nobs));

  %do i=1 %to &nobs;

             %let varnume=%sysfunc(varnum(&dsid,var2));

             %let variable=%sysfunc(getvarc(&dsid,&varnume));

%let m=%eval(&m.+1);

%put &m.;

proc datasets lib=uat nolist;

modify develop_final3;

rename&variable=flt2_num_var_noms_&m.;

quit;

%end;

%end;

  %let dsid=%sysfunc(close(&dsid));

  %end;

%mend chg_name;

%chg_name;

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Variable datasets sysfunc Develop dataset

沙发
邓贵大 发表于 2013-5-10 23:09:50
My guess: you cannot modify uat.develop_final3 in proc datasets while it's been opened in the macro already.
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

藤椅
yongyitian 发表于 2013-5-11 09:11:38
Changed a few places, including using  nvars, varname, and data-step rename

data class;
    set sashelp.class;
run;

%macro chg_name();
  %let dsid=%sysfunc(open(class));
  %if &dsid gt 0 %then %do;
       %let n_vars=%sysfunc(attrn(&dsid,nvars));
       %do i=1 %to &n_vars;
          %let var&i=%sysfunc(varname(&dsid, &i));
       %end;
  %end;
  %let dsid=%sysfunc(close(&dsid));
       %do i = 1 %to &n_vars;
           data class;
           set class;
           rename &&var&i=flt2_num_var_noms_&i;
           run;
       %end;
%mend chg_name;
%chg_name;

板凳
jingju11 发表于 2013-5-11 10:16:49
proc contents and datasets 都是效率好的过程。原因是不读取具体的数据值。虽然我见过有人有PROC CONTENTS DATA =A(OBS =1)等等的写法,但我认为这很可能是多余的。当然是个人观点。京剧
  1. %let dset=uat.develop_final3;
  2. proc contents data =&dset. varnum out=_contents noprint;
  3. proc sql noprint;
  4.   select cats(name,'=flt2_num_var_noms_', varnum) into :names separated by ' ' from _contents;
  5. proc datasets lib =%scan(&dset,1) nolist;
  6.   modify %scan(&dset,2);
  7.   rename &names.;
  8. run;quit;
复制代码

报纸
邓贵大 发表于 2013-5-11 10:19:36
yongyitian 发表于 2013-5-11 09:11
Changed a few places, including using  nvars, varname, and data-step rename

data class;
no offense but try to avoid loops in macro if possible
  1. proc contents data=sashelp.class out=hell(keep=name varnum) varnum;
  2. proc sql noprint;
  3.         select cats(name, '=flt2_num_var_noms_', varnum) into :helllist separated by ' '
  4.         from hell
  5.         order by varnum;
  6. quit;

  7. proc datasets library=sashelp nolist;
  8.         modify class;
  9.         rename &helllist;
  10. quit;
复制代码
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

地板
yongyitian 发表于 2013-5-11 12:00:00
Agree with upper floors that using proc contents and proc datasets are the accepted methods for renaming dataset variables.

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

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