SAS基础编程.docx

上传人:b****7 文档编号:10180738 上传时间:2023-02-09 格式:DOCX 页数:22 大小:22.43KB
下载 相关 举报
SAS基础编程.docx_第1页
第1页 / 共22页
SAS基础编程.docx_第2页
第2页 / 共22页
SAS基础编程.docx_第3页
第3页 / 共22页
SAS基础编程.docx_第4页
第4页 / 共22页
SAS基础编程.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

SAS基础编程.docx

《SAS基础编程.docx》由会员分享,可在线阅读,更多相关《SAS基础编程.docx(22页珍藏版)》请在冰豆网上搜索。

SAS基础编程.docx

SAS基础编程

第三课SAS编程–第一部分

一.SAS变量的定义

/*直接赋值*/

datanewvar;

a1=100;

a2=1.2e-5;/*数值型变量的科学表达法*/

b=0100;/*数值型变量前面的0不起作用*/

c=‘new’;

d=“NEW”;/*字符型变量输入时是什么,值就是什么*/

f=“NeW“;

name1=“Tom’s”;

name2=‘Tom’’s’;

date1=‘1jan2006’d;/*直接定义日期*/

date2=‘01jan04’d;

time1=‘9:

25’t;/*直接定义时间*/

time2=‘9:

25:

19’t;

dtime=‘18jan2003:

9:

27:

05am’dt;/*定义日期时间*/

ifbegin=’01may04:

9:

30:

00’dtthenend=”31dec90:

5:

00:

00”dt;

run;

procprint;run;

/*自定义变量*/

/*产生一个取值为1到100的变量*/

dataint;

doi=1to100;/*此处do…end为循环语句,i为循环指标,可取任何符号*/

a=i;

output;

end;

run;

procprint;run;

/*简洁版*/

dataint;

doa=1to100;

output;

end;

run;

procprint;run;

 

/*通过各种运算定义变量*/

SAS算子

✧用于比较:

=(EQ)等于,^=(NE)不等于,~=(NE)不等于,>(GT)大于,<(LT)小于,>=(GE)大于等于,<=(LE)小于等于

✧算数运算:

+加法,-减法,*乘法,/除法,**幂次

✧逻辑运算:

&(AND)和,|(OR)或,~(NOT)非,^(NOT)非

 

/*旅游数据*/

datatravel;

inputcountry$nightsaircostlandcostvendor$;

cards;

France8793575Major

Spain10805510Hispania

India10.489Royal

Peru7722590Mundial

;

run;

datanewair;

settravel;

lengthremarks$30;/*remark的值会很长。

所以事先设定长度*/

newacost=aircost+10;/*计算新的机票价格*/

ifvendor='Hispania'thenremarks='Bonusfor10+people';

elseifvendor='Mundial'thenremarks='Bonuspoints';/*对航线加说明*/

elseifvendor='Major'thenremarks='Discountfor30+people';

ifcountry='Peru'thendelete;/*去掉一个已经停飞的航线*/

run;

 

/*另一个旅游数据*/

dataarttour;

inputcity$1-9nights11landcost13-16events18

describe$20-36guide$38-45backup$47-54;

cards;

Rome375074M,3GD'AmicoTorres

Paris8168065M,1otherLucasLucas

London6123053M,2GWilsonLucas

NewYork6.85M,1G2otherLucasD'Amico

Madrid337053M,2otherTorresD'Amico

Amsterdam458063M,3GVandever

;

run;

datanewarttour;

setarttour;

ifcity='Rome'thenlandcost=landcost+30;/*修改旅游信息*/

ifevents>nightsthencalendar='Checkschedule';

elsecalendar='Noproblems';

ifguide='Lucas'andnights>7thenguide='Torres';

 

iflandcost>=1500thenprice='High';/*定义新变量price*/

elseiflandcost>=700thenprice='Medium';

elseprice='Low';

ifevents/nights>2thenpace='Toofast';

elsepace='OK';

if(city='Paris'orcity='Rome')and(guide='Lucas'or

guide="D'Amico")thentopic='Arthistory';

iflandcostgt1500orlandcost/nightsgt200thenlevel='Deluxe';

ifupcase(city)='MADRID'thenguide='Duncan';/*使用upcase函数*/

ifguide<=:

'L'thengroup='A-L';/*如果guide以L开头*/

elsegroup='M-Z';

/*使用index函数,注意if的用法*/

ifindex(describe,'other')thenotherev='Yes';

elseotherev='No';

run;

procprintdata=newarttour;title'Tourinformation';run;

/*通过SAS函数定义变量*/

/*函数ranuni生成服从均匀分布的随机数*/

datarandom(drop=i);

doi=1to3;

Y1=ranuni(1298573062);

Y2=ranuni(447801538);

Y3=ranuni(631280);

output;

end;

procprintdata=random;title“Asinglestreamacrossmultiplevariables”;

run;

/*函数anyupper给出一个字符串中开始位置后的第一个大写字母的位置*/

data_null_;

string=‘Next=_n_+12E3’;

j=0;

dountil(j=0);

j=anyupper(string,j+1);

ifj=0thenput+3“That’sall”;

elsedo;

c=substr(string,j,1);

put+3j=c=;

end;

end;

run;

 

/*Cakedata*/

datacake;

inputLastName$1-12Age13-14PresentScore16-17

TasteScore19-20Flavor$23-32Layers34;

datalines;

Orlando279380Vanilla1

Ramey328472Rum2

Goldston466875Vanilla1

Roe387973Vanilla2

Larsen237784Chocolate.

Davis518691Spice3

Strickland198279Chocolate1

Nguyen577784Vanilla.

Hildenbrand338183Chocolate1

Byron627287Vanilla2

Sanders265679Chocolate1

Jaeger436674Chocolate2

Davis286975Vanilla1

Conrad698594Chocolate2

Walters556772Spice2

Rossburger287881Chocolate2

Matthew428192Spice2

Becker366283Chocolate1

Anderson278785Chocolate1

Merritt627384Vanilla2

;

PROCPRINT;TITLE"DataCake";RUN;

datacake;

setcake;

score=sum(PresentScore,TasteScore)*sqrt(layers);

score=round(score,.1);

Lastname=left(upcase(lastname));

Lastname1='Mr.orMs.'||Lastname;

DropPresentScoreTasteScoreLayers;

labelscore="FinalScore"

flavor="CakeFlavor";

run;

Procprint;run;

 

二.循环语句

/*vacationdata*/

datavacation;

inputidyears;

datalines;

12

23

35

46

67

78

;

run;

 

datavacation;

setvacation;

ifyears>5thendo;

weeks=3+year/5;

group='Qualified';

end;/*这里doend不是循环语句,是大括号*/

elsedo;

yrsleft=5-years;

group='NotQualified';

end;

run;

 

procprint;run;

/*Investmentdata*/

datainvestment;

begin='01JAN1990'd;

end='31DEC2009'd;

capital=0;

doyear=year(begin)toyear(end);/*函数year给出时间变量中的年的值*/

capital=Capital+2000+.07*(Capital+2000);

output;

end;

put'ThenumberofDATAstepiterationsis'_n_;

run;

procprintdata=investment;formatCapitaldollar12.2;run;

 

datainvestment;

begin='01JAN1990'd;

end='31DEC2009'd;

doyear=year(begin)toyear(end);

Capital+2000+.07*(Capital+2000);/*自动给出初值和更新变量值*/

output;

end;

put'ThenumberofDATAstepiterationsis'_n_;

run;

procprintdata=investment;formatCapitaldollar12.2;run;

/*anotherexample*/

dataa;

inputx;

datalines;

5

000

2500

;

run;

 

datab;

seta;

a=normal(0);/*normal函数产生一个正态随机数,0表示取现在的时间作为seed*/

exit=10;

doi=1toexit;

y=x*normal(0);

ify>25theni=exit;

output;

end;

run;

procprint;run;

 

三.SAS的分组-By语句

/*by语句的定义*/

datazipcodes;

inputstate$1-2city$5-14street$15-25zipcode30-34;

cards;

AZTucsonDomenicLn85730

FLMiamiTradeAve33133

FLMiamiThomasAve33133

FLLakelandFrenchAve33801

FLMiamiNerviaSt33146

AZTucsonGleesonPl85730

FLMiamiRicest33133

FLMiamiCorsicaSt33146

FLLakelandEgretDr33809

FLMiamiSurreyDr33133

;

run;

procprint;run;

/*将同一个州的观测值放一起,by语句的变量为state*/

procsortdata=zipcodesout=state;bystate;run;

procprint;run;

/*进一步将州内的城市排序,by语句的变量为statecity*/

procsortdata=zipcodesout=city;bystatecity;run;

procprint;run;

/*所以变量都排序,by语句的变量为statecityzipcodestreet*/

procsortdata=zipcodesout=city;bystatecityzipcodestreet;run;

procprint;varstatecityzipcodestreet;run;

/*将state按逆序排列*/

procsortdata=zipcodesout=city;

bydescendingstatecityzipcodestreet;

run;

procprint;varstatecityzipcodestreet;run;

/*将state和city都按逆序排列*/

procsortdata=zipcodesout=city;

bydescendingstatedescendingcityzipcodestreet;

run;

procprint;varstatecityzipcodestreet;run;

/*将数据按zipcode分组打印*/

procprint;varstatecityzipcodestreet;bystatecityzipcode;run;

/*procsort的另一个选项*/

datadupobs;

inputcountry$1-11tourtype$13-24nightslandcostvendor$;

cards;

Spainarchitecture10510World

Japanarchitecture8720Express

Switzerlandscenery9734World

Switzerlandscenery9734World

Francearchitecture8575World

Irelandscenery7558Express

NewZealandscenery161489Southsea

Italyarchitecture8468Express

Greecescenery12698Express

;

run;

procprintdata=dupobs;title'DataSetDUPOBS';run;

procsortdata=dupobsout=fixednoduplicates;bycountry;run;

procprintdata=fixed;

title'RemovingaDuplicateObservationwithPROCSORT';

run;

/*确定由by语句定义的组的边界*/

/*当使用procsort,系统会自动生成first.varible和last.variable*/

procsortdata=zipcodes;bystatecity;run;

data_null_;/*不生成数据,但使用datastep*/

setzipcodes;

bystatecity;

if_N_=1thenput"Groupedbystatecity";/*将信息写到log窗口*/

put;

put_N_=state=city=first.state=last.state=first.city=last.city=;

put;/*打一个空行*/

run;

 

/*计算每组的工资和*/

datasalaries;

inputDepartment$Name$WageCategory$WageRate;

datalines;

BADCarolSalaried20000

BADElizabethSalaried5000

BADLindaSalaried7000

BADThomasSalaried9000

BADLynneHourly230

DDGJasonHourly200

DDGPaulSalaried4000

PPDKevinSalaried5500

PPDAmberHourly150

PPDTinaSalaried13000

STDHelenHourly200

STDJimSalaried8000

;

procprintdata=salaries;run;

procsortdata=salariesout=temp;byDepartment;run;

databudget(keep=DepartmentyearlywagePayroll);

settemp;

byDepartment;

retainpayroll;

ifWageCategory="Salaried"thenYearlyWage=WageRate*12;

elseifWageCategory="Hourly"thenYearlyWage=WageRate*2000;

/*SASsetsFIRST.variableto1ifthisisanewdepartmentintheBYgroup*/

iffirst.DepartmentthenPayroll=0;

payroll=payroll+YearlyWage;

/*SASsetsLAST.variableto1ifthisisthelastdepartmentintheBYgroup*/

/*iflast.Department;*//*只需取一组中最后一个观测值*/

run;

procprintdata=budget;

formatPayrolldollar10.;

title“AnnualPayrollbyDepartment”;

run;

 

/*求身高的平方和*/

DATADEMO;

INPUTGENDER$HEIGHTWEIGHTAGE;

DATALINES;

M6815523

F619920

F6311521

M7020545

M69170.

F6512530

M7222048

;

Run;

PROCPRINT;RUN:

dataa;

setDEMOend=done;/*done是一个指示变量,对最后一个观测值为1,其他为0*/

retainNsum0;

N=N+1;

Sum=sum+height**2;

ifdonethendo;

mean=sum/N;

output;

end;

run;

procprint;run;

 

datadonkey;

inputyear1-4pres$6-29vicepres$31-55result$60-64;

cards;

1920JamesM.CoxFranklinD.Rooseveltlost

1924JohnW.DavisCharlesW.Bryanlost

1928AlfredE.SmithJosephT.Robinsonlost

1932FranklinD.RooseveltJohnN.Garnerwon

1936FranklinD.RooseveltJohnN.Garnerwon

1940FranklinD.RooseveltHenryA.Wallacewon

1944FranklinD.RooseveltHarryS.Trumanwon

1948HarryS.TrumanAlbenW.Barkleywon

1952AdlaiE.StevensonJohnJ.Sparkmanlost

1956AdlaiE.StevensonEstesKefauverl

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1