人大经济论坛SAS版的几个问题

1、利用proc format对坐标轴刻度微调
goptions reset=all;
proc format;
value vfmt 0,4,8,12,13 =[2.] other='';
run;
axis1 minor=(number=1) label=none order=( 0 to 13 by 1 );
data one;
input y x;
cards;
2 0
3 7
4 9
2 13
6 8
5 2
;
run;
proc gplot data=one;
plot y*x/haxis=axis1;
format x vfmt.;
run;quit;


2、SAS中导出标签到excel

/*Mike Zdeb 的方法*/
ods results off;
ods listing close;
ods csv file='c:\air.csv';
proc print data=sashelp.air label noobs;
run;
ods csv close;
ods results on;
ods listing;

/*dload也可以*/
proc dbload data=sashelp.air dbms=xls;
     path="c:\temp";
     putnames yes;
     limit=0;
     label;
     reset all;
     load;
run;


3、把SAS数据集按某个字段分组导出到excel

data  a;
input  x  y $;
cards;
1   j
1   k
2   l
2   j
3   p
4   u
3   r
;

/* macro */
%macro splitfile;
%do i=1 %to 4;
   data part;
     set a;
     if x eq &i.;
   run;
   PROC EXPORT DATA= WORK.part
       OUTFILE= "c:\test&i..xls"
       DBMS=EXCEL REPLACE;
   RUN;
%end;
%mend splitfile;
%splitfile

/* ods */
proc sort data=a out=b;
   by x;run;
ods tagsets.excelXP file="c:\new.xls";
proc print data=b;
   by x;
run;
ods tagsets.excelXP close;


4、在一个表中删除出现在另一表中的记录,有没有表示整条记录的语句

*模拟lz的数据要求,从class随机抽取5个数组成class1*/
data class;
   set sashelp.class;
run;
proc surveyselect data=class 
   method=srs n=5
   seed=34234 out=class1;
run;
/*删除存在class1中数据后的class数据保存至last*/
proc sql;
   create table last as
   select *
      from class
   except all
   select * 
      from class1;
quit;

/*data步*/
data need;
    merge class class1(rename=(name=name1));
    if name1=' ' then output;
    drop name1;
run;

5、如何计算某个值连续出现的次数,例如找出obs连续取值为1最长的值

/* hopewell的代码 */
data _null_;
    input obs x1-x9;
    array arr x1-x9;
    do over arr;
        if arr then do;
            temp+arr;
            max=max(max,temp);
        end;
        else temp=0;
    end;
    put obs= max=;
datalines;
1 0 0 0 0 0 1 1 0 1
2 0 1 1 1 0 0 1 1 0
;

 

6、数据集相减(相当于减去数据集的部分数据)

data a;
input x y;
cards;
1 2
3 4
5 6
7 8
9 0
;
data b;
input x y;
cards;
1 2
3 4
7 8
;
run;
proc sql;
create table need as
select a.*
from a as a
left join 
  b as b
  on a.x=b.x
  where missing(b.x);
quit;

/* or */
proc sql noprint;
create table new as
   select x,y
      from a
      where x not in
         (select x from b);
quit;

/* or */

data c;
merge a b(in=in1);
by x y;
if not in1;
run;
/*or*/

proc sql;
select * from a
except
select * from b;
quit;

 

7. 创建多个哑变量

data a;
input id  type $;
cards;
1   a
2   b
3   a
4   c
5   c
;
run;
/* bool */
data b;
set a;
dmy1=(type='a');
dmy2=(type='b');
dmy3=(type='c');
run;
/*proc glmmod*/
proc glmmod data=a OUTDESIGN=c;
class type;
model id=type/noint;
run;
/*proc sql*/
proc sql;
create table d as
select id,
       unique.type as _name_,
           a.type=unique.type as dummy
from a,(select distinct type as type from a) as unique
order by id ,_name_;
quit;
proc transpose data=d out=dummies(drop= _name_);
by id ;
var dummy;
run;

 

8.两数据,相同的id,则对应数据相加
data a;
input id $ x1 x2 x3;
cards;
1      8     20   3
3      3     0     8
4      2     4     0
;
data b;
input id $ x1 x2 x3;
datalines;
1    0     7    3
4    9    0     5
5    8    5     9
;
data c;
   set a b;
run;
proc sql;
create table d as
select distinct id,
       sum(x1) as x1,
           sum(x2) as x2,
           sum(x3) as x3
from c
group by id;
quit;
/*proc summary 替换proc sql*/
proc sort data=c;
by id;
run;
proc summary data=c;
var x1 x2 x3;
by id;
output out=d(drop=_freq_ _type_) sum=;
run;
/* merge 然后sum*/
data want(drop=x11 x22 x33);
  merge a b(rename=(x1=x11 x2=x22 x3=x33));
  by id;
  x1=sum(x1,x11);x2=sum(x2,x22);x3=sum(x3,x33);
run;
/*proc sql coalesce连接*/
proc sql;
  select coalesce(a.id,b.id) ,sum(a.x1,b.x1) as x1 ,sum(a.x2,b.x2) as x2,sum(a.x3,b.x3) as x3
  from a
  full join b on a.id=b.id
  order by 1  ;
quit;

 

9.数据中id相同的观察中间添加空行

/*以sashelp.stocks数据为例,stock相当于你的code*/
dm "out;file c:\result.txt;";
data new;
   set sashelp.stocks;
   by stock;
   output;
   if stock=lag(stock) then do;
      if mod(_n_,1)=0;
      array allnums {*} _numeric_ ;
      array allchar {*} _character_ ;
      drop i;
      do i=1 to dim(allnums); allnums{i}=.; end;
      do i=1 to dim(allchar); allchar{i}=' '; end;
      output;  /* Output blank observation */
   end;
run;


10.循环取数据的几行生成新的数据

%macro splitfile(num);
    data _null_;
       if 0 then
           set YOUTDATA nobs=count;
        call symput('numobs',put(count,8.));
    run;
    %let m=%sysevalf(&numobs/&num,ceil);
    data %do j=1 %to &m;
        class_&j
        %end;
        ;
        set YOURDATA;
        %do i=1 %to &m;
            if %eval(&num*(&i-1)) <_n_ <= %eval(&num*&i) then
                output YOURDATA_&i;
        %end;
    run;
%mend splitfile;
%splitfile(91);
/* ceil ,call execute */
data _null_;
        if 0 then set sashelp.class nobs=nobs;
        length dslist $100;
        size=6;
        dsnum=ceil(nobs/size);
        do i=1 to dsnum;
           dslist=cat(strip(dslist)," ds"||strip(i));
        end;
    call execute("data "||trim(dslist)||";");
        call execute("    set sashelp.class;");
        do i=1 to dsnum;
           call execute( "   if "||strip((i-1)*size)||" <_n_<= "||strip(i*size)||" then output ds"||strip(i)||";");
        end;
        call execute("run;");
run;
/* hash */
data _null_ ;
  dcl hash hh   (             ) ;
  hh.definekey  ('k'          ) ;
  hh.definedata ('sex', 'name', 'age', 'height', 'weight') ;
  hh.definedone () ;
  do until(mod(k,5)=0 or last);
  k+1;
  set sashelp.class end=last ;
  hh.add();
  end;
  gp+1;
  hh.output(dataset: 'a'||strip(gp));
run;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值