第一次接触sas编程

第一次的代码学习,一点点的小感受,一起来分享

简单的运用,趁着我还记得,先说几个快捷键

1、F3:对选中的程序进行运行

     F5:对全部的程序给予运行

     ctrl+shift+/:将锁定的程序进入运行的进程之中

     ctrl+/:将程序锁定,无论何时也不参与运行

2、宏变量的设置与使用

     宏变量可以使一个比较麻烦的长值字符串瞬间转化为非常简单的赋值变量,可以用很简单的几个词就可以得到想要的结果。

     赋值的表达式为:%let  变量名=想让变量代替的值(这里是唯一确定的字符串)

     使用宏变量,要对变量进行的使用"&宏变量.",重点&"".,永远不容忽视

     %let path=路径or字符串值

3、填补缺失值,对原有的变量重新编入新的变量

     if missing(缺失的字段名称) then
           do;
                新生成的字段名称='Unknown';
           end;
     else 新生成的字段名称=原来的字段名称;

     (else if 原来的字段名称=0 then
           do;
                新生成的字段名称='0';
           end;
     else if 原来的字段名称=1 then
           do;
                新生成的字段名称='1';
           end;)括

      号内的语句和上面的效果一致,若是变量都有变化,用下面会更好;若是变量不变且有较多的取值时,最好是用上面的编程语句,简单速度。

4、连续型变量的拆分及语句的划分   

    proc rank data=build_sample(keep=输出变量1   输出变量2) out=temp3(输出的表名)   group=10(分组);     

           var em_months_last_open;    赋新的变量名

           ranks rank;      排序

    run;

   sas 中调用 mysql 的 select 语句 

   proc sql;   

         select rank,     min(em_months_last_open) as min,      max(em_months_last_open) as max      from temp3      group by 1     order by 1 ;

  quit;

  data temp4;     所得值放入新表,新表名字为temp4

  set temp3;       从上图所得表temp3中抽取信息

  format  category $30.;   格式设置为变量30的数值型

     下句为if的条件循环语句,将所有的个例全部划分到其10个分组之中。

              if   missing(em_months_last_open)  then            

                   do;                

                        category='Unknown';                 

                                bin=1;      

                    end;   

             else   if   em_months_last_open<=0.131422     then           

                  do;           

                        category='LOW-0. 131422';           

                                bin=2;          

                  end;                     

          else    if     em_months_last_open<=0.406213 then      

                 do;   

                       category='0.131422<-0.406213';            

                               bin=3;        

                 end;   

         else     if    em_months_last_open<=1.116487  then         

                  do;         

                        category='0.406213<-1.116487';       

                                bin=4;   

                 end;  

        else    if       em_months_last_open<=2.555556    then       

                 do;                       

                      category='1.116487<-2.555556';              

                               bin=5;        

                end;    

       else   if   em_months_last_open<=5.318399    then     

                do;        

                      category='2.555556<-5.318399';            

                              bin=6;       

              end;     

      else  if   em_months_last_open<=9.715651   then       

               do;             

                      category='5.318399<-9.715651 ';          

                              bin=7;     

               end;  

      else  if   em_months_last_open<=15.48566  then        

               do;          

                        category='9.715651<-15.48566';     

                                bin=8;        

              end;   

     else  if  em_months_last_open<=22.73417 then      

              do;           

                         category='15.48566<-22.73417';            

                                  bin=9;      

             end;    

    else   if   em_months_last_open<=37.29152 then        

              do;           

                         category='22.73417<-37.29152';           

                                bin=10;   

             end;     

   else   if   em_months_last_open>37.29152    then        

            do;          

                          category='37.29152-HIGH';           

                                  bin=11;      

           end;

   run;

最后再用一个sql语句 计算我所想要的值,恰恰也生成了一个新的变量

   proc sql;     

        create  table  (新表名字)profile_em_months_last_open   as    select   'em_months_last_open'    as     var,  category,   

              count(*) as count ,    sum(dv_response) as responders,      count(*)/&total_cnt.前面赋值的一个宏变量   format percent9.1 as percent,    

              sum(dv_response)/count(*)  as Response_rate,    100*(sum(dv_response)/count(*))/&avg_rr.前面赋值的一个宏变量  as index           

       from  temp4        group by 1,2             

          order by 1,2 ; group by 分别指不同的字段变量,这里表示的是var和category。

   quit;

 

5、 变量的重要性最好控制在30%以内,这样方才说明你的变量选择是正确的,因为很有可能你的一个变量很大程度能决定你的结果,这样的结果反而是不好的,同时也能说明你落下了一些重要的自变量,即你的自变量是不足的。

6、concents,属性,内容

          proc contents data=dt.response_model;
    run

    看看表的字段变量的属性

 

posted @ 2016-06-08 20:04  kelsey_yuan  阅读(201)  评论(0编辑  收藏  举报