• SAS--chapter11data子集(if 、select、 delete)


    libname clinic 'D:sas';
    filename test 'D:sas	est.txt';   *纯文本;
    data clinic.yb;
    infile test;
    input sex $1-2 weight 3-6 height 7-9;  *和顺序有关,查看test.txt;
    retain hh 2;            *hh 从2开始加,默认是0;
    hh+height;            *创建新变量hh,且不需要关键词开头,遇到缺失值直接跳过;
    if weight>80 then description ='heave';     *if weight<=80,description will be missing;
    RUN;
    
    data clinic.data2;
        infile test;
        input sex $1 weight 3;
        retain w 4;
        w+weight;
    run;
    
    
    if test<85 and time<=20
        then Status='RETEST';
    
    if region in ('NE','NW','SW')
        then Rate=fee-25;
    
    if target gt 300 or sales ge 50000
        then Bonus=salary*.05;
    
    if status='OK' and type=3
        then Count+1;
    
    if (age^=agecheck | time^=3)
        & error=1 then Test=1;
    
    if (age^=agecheck | time^=3)
    & error=1 then Test=1;
    
    if status='S' or cond='E'
        then Control='Stop';
    
    if not(loghours<7500)
        then Schedule='Quarterly';
    
    if region not in ('NE','SE')
        then Bonus=200;
    
    if status='OK' and type=3
        then Count+1;
    
    if status='S' or cond='E'
        then Control='Stop';
    
    if not(loghours<7500)
        then Schedule='Quarterly';
    
    if region not in ('NE','SE')
        then Bonus=200;
    
    *数字0 和 missing 为false,其他数字为true;
    if x=1 or 2;   *always true;
    if x=1 or x=2;  *right;
    
    if totaltime>800 then TestLength='Long';
    else if 750<=totaltime<=800 then TestLength='Normal';   *esle if 比 多个if then  连用节省系统资源;
    else if totaltime<750 then TestLength='Short';
    
    filename test 'D:sas	est.txt';   *纯文本;
    data clinic.yb;
    infile test;
    input sex $1 weight 3-5 height 7-8;  
    if weight<170 then description='short';  *出现的这一列内容width以该变量赋的第一个值为准;
    else if 170<=weight<=180 then description='Normal';
    else if weight>180 then   description='longgg';
    RUN;
    
    length Type $ 8;
    length Address1 Address2 Address3 $ 200;
    length FirstName $ 12 LastName $ 16;
    
    data clinic.yb;
    infile test;
    input sex $1 weight 3-5 height 7-8;  
    length description $ 10;              *给新变量赋上长度,length出现在变量初始化之前;
    if weight<170 then description='short'; 
    else if 170<=weight<=180 then description='Normal';
    else if weight>180 then   description='longgg';
    RUN;
    
    data clinic.yb;
    infile test;
    input sex $1 weight 3-5 height 7-8;  
    if weight<170 then delete;  *删除满足if的observations;
    RUN;
    
    
    data clinic.yb (drop=height);  *去掉某些列 dataprint均可用;
    infile test;
    input sex $1 weight 3-5 height 7-8;  
    RUN;
    
    data clinic.yb ;
    infile test;
    input sex $1 weight 3-5 height 7-8;  
    drop  height;                 *方式2  !!drop keep只能用在data步,proc不可用;
    RUN;
    
    data clinic.yb ;
    infile test;
    input sex $1 weight 3-5 height 7-8;  
    label height='H';      * 该变量名 ,data步为永久的,print步为临时的;
    format weight comm1.2  *;
    RUN;
    
    
    *当变量为数值型且 互斥时,select 比if then 高效;
    filename test 'D:sas	est1.txt';   *纯文本;
    
    data clinic.data1 ;
    infile test;
    input sex $1 weight 3;  
    select (weight);
    when (1) x='';
    when (2) x='';
    when (3) x='';
    when (4) x='';
    when (5) x='';
    when (6) x='';
    otherwise;
    end;
    RUN;
    *or;
    data clinic.data1 ;
    infile test;
    input sex $1 weight 3;  
    select (weight);
    when (1) x='';
    when (2) x='';
    when (3) x='';
    when (4) x='';
    when (5) x='';
    otherwise (6) x='';
    end;
    RUN;
    
    data clinic.data1 ;
    infile test;
    input sex $1 weight 3;  
    select;
    when (weight=1) x='';
    when (weight=2) x='';
    otherwise ;
    end;
    RUN;
    
    *do end 组合,对满足某一条件的数据执行多个操作;
    data clinic.data1 ;
        infile test;
        input sex $1 weight 3;  
            select (weight);
            when (1) x='';
            when (2) x='';
            when (3) 
                do;
                    x='';
                    y='middle';
                end;
            when (4) x='';
            when (5) x='';
            otherwise put 'problem';   *不能(6)='', 什么都不写 or put;
            end;
    RUN;
    
    data clinic.data2;
        infile test;
        input sex $1 weight 3;
            if weight=1 then x='';
            else if weight=2 then 
            do; 
                x='';
                y='middle';
            end;
    run;
    libname clinic "E:sas";
    filename saledata "e:sas	est.txt";
    data clinic.sales(drop=sex);
       infile saledata;
       input name $ 1-10 sex $ 12-13 age 15 height 17-19 weight 21-23;
       Total=height+weight;
       retain CumTotal 1254657;
       cumtotal+total;
       if sex="" then delete;
       if cumtotal>1255500 then 
        do;
        testlength="short";
        y=1;
        end;
       else if 1254900<=cumtotal<1265000 then testlength="long";
       else testlength="1234567";  *只有12345,因为testlength录入的第一个值为short,只有五个字符;
    run;
    proc print data=clinic.sales;
    run;
    Valar morghulis
  • 相关阅读:
    VS2010 修改模板文件,增加默认注释
    Substring方法(C#,JS,Java,SQL)的区别
    好的架构是进化来的,不是设计来的
    SQL Server编程系列(2):SMO常用对象的有关操作
    (转) SQL Server编程系列(1):SMO介绍
    SQL Server的分页优化及Row_Number()分页存在的问题
    CTE(Common Table Expression) 公用表表达式
    SSH加密原理、RSA非对称加密算法学习与理解
    asp.net 项目目录说明
    【转】SOA架构设计经验分享—架构、职责、数据一致性
  • 原文地址:https://www.cnblogs.com/super-yb/p/11679416.html
Copyright © 2020-2023  润新知