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;