楼主: lnlhckao123
2789 6

[问答] 请问怎样用sas aql语句在数据集任何一个位置插入一个变量 [推广有奖]

  • 0关注
  • 3粉丝

副教授

61%

还不是VIP/贵宾

-

威望
0
论坛币
14832 个
通用积分
9.1001
学术水平
7 点
热心指数
4 点
信用等级
4 点
经验
12566 点
帖子
754
精华
0
在线时间
399 小时
注册时间
2010-8-21
最后登录
2024-2-8

200论坛币
请问怎样用sas aql语句在数据集任何一个位置插入一个变量,请举实例,谢谢!

最佳答案

yongyitian 查看完整内容

/* This is only a demo */ /* practically for a large number of variables, it is much simple to select all the variables */ /* through PROC SQL FEEDBACK */ %macro mymacro(lib=, Old_table=, New_table=, NewVar=, col=, formular=); proc sql noprint; select name into: a_var separated by ', ' from dictionary.columns where libname="%upcase(&lib)" and memna ...
关键词:数据集
即使在人大经济论坛这个网络世界,我仍以真诚为基础与我的好友进行交往!
沙发
yongyitian 发表于 2013-5-12 23:21:32 |只看作者 |坛友微信交流群
/* This is only a demo */
/* practically for a large number of variables, it is much simple to select all the variables */
/* through PROC SQL FEEDBACK */  


%macro mymacro(lib=, Old_table=, New_table=, NewVar=, col=, formular=);
proc sql noprint;
   select name into: a_var separated by ', '
      from dictionary.columns
         where  libname="%upcase(&lib)" and
                memname="%upcase(&old_table)" and
                varnum < &col;
   select name into: b_var separated by ', '
      from dictionary.columns
         where  libname="%upcase(&lib)" and
                  memname="%upcase(&old_table)" and
                  varnum ge &col;
   select max(varnum) into: nv
      from dictionary.columns
           where  libname="%upcase(&lib)" and memname="%upcase(&old_table)";
        %if &col = 1 %then %do;
            create table &new_table as
              select &formular as &newVar, &b_var
                from &lib..&old_table; %end;
        %if &col > &nv %then %do;
            create table &new_table as
              select &a_var, &formular as &newVar
                from &lib..&old_table;  %end;
        %if &col > 1 and &col <= &nv %then %do;
            create table &new_table as
              select &a_var, &formular as &newVar, &b_var
                from &lib..&old_table;  %end;
quit;
%mend mymacro;

/* example 1:  add a new variable calculating height in meter  */
%mymacro(lib=sashelp, old_table=class, new_table=class2,
         NewVar=height_in_metter, col=5, formular=height*0.0254);

data aaa;    /* create the sample dataset, before running the following code */
  retain x1-x6 (1 2 3 4 5 6);
  output; output; output;
run;

/* example 2: calculate sum of x1 and x2 and insert it as column 3 */
%mymacro(lib=work,  old_table=aaa, new_table=bbb,   
         NewVar=sum_x1x2, col=3, formular=sum(x1,x2));

/* example 3: calculate sum of all variables and insert it as the last column */
%mymacro(lib=work,  old_table=aaa, new_table=ccc,
         NewVar=sum_all, col=7, formular=sum(x1,x2,x3,x4,x5, x6));

/* example 4: calculate sum of variable 5  and put as the 6th column */
%mymacro(lib=work,  old_table=aaa, new_table=ddd,
         NewVar=x5_total, col=6, formular=sum(x5));

/* example 5: insert a character at column 1 of the new table */
%mymacro(lib=work,  old_table=aaa, new_table=eee,
         NewVar=Group, col=1, formular='A');

proc print data=class2; title 'class2'; run;
proc print data=bbb;    title 'bbb'; run;
proc print data=ccc;    title 'ccc'; run;
proc print data=ddd;    title 'ddd'; run;
proc print data=eee;    title 'eee'; run;

已有 2 人评分学术水平 热心指数 信用等级 收起 理由
lnlhckao123 + 5 + 3 + 3 热心帮助其他会员
playmore + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 6  热心指数 + 4  信用等级 + 4   查看全部评分

使用道具

藤椅
bobguy 发表于 2013-5-13 00:11:05 |只看作者 |坛友微信交流群
See title.

data t1;
  retain x1-x5 (1 2 3 4 5);
  output;
  run;
title "insert a variable at the begining position";
proc sql;
  select sum(x1,x2) as y, *
  from t1;
  quit;

title "insert a variable at the last position";
proc sql;
  select *, sum(x1,x2) as y
  from t1;
  quit;

title "insert a variable at the position you specified";
proc sql;
  select x1,x2, sum(x1,x2) as y, x3,x4,x5
  from t1;
  quit;
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
lnlhckao123 + 2 + 2 + 2 热心帮助其他会员

总评分: 学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

使用道具

板凳
邓贵大 发表于 2013-5-13 10:13:36 |只看作者 |坛友微信交流群
  1. %let position=3;
  2. %let dataset = sashelp.class;
  3. %let newdataset = xxx;
  4. %let newvar = weight*703/height**2 as bmi;
  5. proc sql noprint;
  6. %global vars_before vars_after;
  7. *vars_before = variable list before poistion 3;
  8. select name into :vars_before separated by ','
  9.         from dictionary.columns
  10.         where upcase(libname) = coalesec(upcase(scan("&dataset", -2, '.')), 'WORK')
  11.                 and upcase(memname)=upcase(scan("&dataset",-1,'.'))
  12.                 and varnum<&position
  13.         order by varnum;
  14. *vars_after = variable list after poistion 2;
  15. select name into :vars_after separated by ','
  16.         from dictionary.columns
  17.         where upcase(libname) = coalesec(upcase(scan("&dataset", -2, '.')), 'WORK')
  18.                 and upcase(memname)=upcase(scan("&dataset",-1,'.'))
  19.                 and varnum>=&position
  20.         order by varnum;
  21. create table xxx as
  22.         select %sysfunc(catx(%str(,), &vars_before, &newvar, &vars_after))
  23.         from &dataset;
  24. quit;
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
lnlhckao123 + 3 + 3 + 3 热心帮助其他会员

总评分: 学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

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.

使用道具

报纸
playmore 发表于 2013-5-14 08:30:29 |只看作者 |坛友微信交流群
yongyitian 发表于 2013-5-13 23:45
/* This is only a demo */
/* practically for a large number of variables, it is much simple to sel ...
好东西,我收藏了
playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

使用道具

地板
yongyitian 发表于 2013-5-14 10:07:53 |只看作者 |坛友微信交流群
playmore 发表于 2013-5-14 08:30
好东西,我收藏了
大侠的macro写的非常专业,一定要仔细拜读。

使用道具

7
lnlhckao123 发表于 2013-5-14 22:23:25 |只看作者 |坛友微信交流群
谢谢各位高手!!
即使在人大经济论坛这个网络世界,我仍以真诚为基础与我的好友进行交往!

使用道具

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

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

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

GMT+8, 2024-5-27 23:40