SQLMarch2007.docx

上传人:b****2 文档编号:2711926 上传时间:2023-05-04 格式:DOCX 页数:61 大小:74.69KB
下载 相关 举报
SQLMarch2007.docx_第1页
第1页 / 共61页
SQLMarch2007.docx_第2页
第2页 / 共61页
SQLMarch2007.docx_第3页
第3页 / 共61页
SQLMarch2007.docx_第4页
第4页 / 共61页
SQLMarch2007.docx_第5页
第5页 / 共61页
SQLMarch2007.docx_第6页
第6页 / 共61页
SQLMarch2007.docx_第7页
第7页 / 共61页
SQLMarch2007.docx_第8页
第8页 / 共61页
SQLMarch2007.docx_第9页
第9页 / 共61页
SQLMarch2007.docx_第10页
第10页 / 共61页
SQLMarch2007.docx_第11页
第11页 / 共61页
SQLMarch2007.docx_第12页
第12页 / 共61页
SQLMarch2007.docx_第13页
第13页 / 共61页
SQLMarch2007.docx_第14页
第14页 / 共61页
SQLMarch2007.docx_第15页
第15页 / 共61页
SQLMarch2007.docx_第16页
第16页 / 共61页
SQLMarch2007.docx_第17页
第17页 / 共61页
SQLMarch2007.docx_第18页
第18页 / 共61页
SQLMarch2007.docx_第19页
第19页 / 共61页
SQLMarch2007.docx_第20页
第20页 / 共61页
亲,该文档总共61页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

SQLMarch2007.docx

《SQLMarch2007.docx》由会员分享,可在线阅读,更多相关《SQLMarch2007.docx(61页珍藏版)》请在冰点文库上搜索。

SQLMarch2007.docx

SQLMarch2007

TopicPROCSQL

1.Introduction

2.GeneratereportsusingPROCSQLandtheappropriateoptions

3.ConstructsubquerieswithinaPROCSQLstep

4.ComparesolvingaproblemusingtheSQLprocedureversususingtraditionalSASprogrammingtechniques

5.AccessDictionaryTablesusingtheSQLprocedure

6.CombiningTablesverticallyusingSetOperationsinPROCSQL

7.JoiningTableshorizontally

8.CreatingandManagingTablesusingPROCSQL

9.CreatingIndexusingPROCSQL

10.DATAView

11.CreatingMacro

12.IntegrityConstraints

13.SQLProceduresPass-ThroughFacility

1.Introduction

SQL,anacronymforStructuredQueryLanguage,isanANSI(AmericanNationalStandardsInstitute)standarddatabaseprogramming/querylanguage.SQLisastandardized,widelyusedlanguagethatretrievesandupdatesdataintablesandviewsbasedonthosetables.

TheSASSystem'sSQLprocedureenablesyouto

∙retrieveandmanipulatedatathatarestoredintablesorviews.

∙createtables,views,andsoforth.

∙createSASmacrovariablesthatcontainvaluesfromrowsinaquery'sresult.

∙addormodifythedatavaluesinatable'scolumnsorinsertanddeleterows.Youcanalsomodifythetableitselfbyadding,modifying,ordroppingcolumns.

∙createtablesandview

∙joinmultipletables

∙generatereports

∙sendDBMS-specificSQLstatementstoadatabasemanagementsystem(DBMS)andtoretrieveDBMSdata.

1.1WhatisSQLtable?

APROCSQLtableissynonymouswithaSASdatafileandhasamembertypeofDATA.YoucanusePROCSQLtablesasinputintoDATAstepsandprocedures.ArowinatableisthesameasanobservationinaSASdatafile.Acolumnisthesameasavariable.

1.2Table-ComparisonofSQLterms,BaseSAS,andDataprocessingterminology:

SQLBaseSASDataProcessing

_______________________________________________________________________

tableSASdatafilefile

rowobservationrecord

columnvariablefield

_______________________________________________________________________

1.3WhyuseSQLorlearnit?

∙Powerful-candomanythings

∙Flexible-justsaywhatyouwant

∙Compatible-youcanusetheSQLyoulearnlaterinotherapplications

∙Required-forSASadvancedprogrammer

1.4SASDataStepandSQL

SASSQL

CreatedatasetCreatetables

UpdatevaluesUpdatevalues

DeleterecordsDeleterecords

AppendnewrecordsUnion/Insert

SortdataSortdata

MergedatasetsJointables

1.5SQLProcedureCodingConventions

SQLstatementsaredividedintoclauses.Forexample,themostbasicSELECTstatementcontainstheSELECTandFROMclauses.ItemswithinclausesareseparatedwithcommasinSQL,notwithblanksasintheSASSystem.

TheSELECTstatement,whichisusedtoretrievedata,alsooutputsthedataautomaticallyunlessyouspecifytheNOPRINToptioninthePROCSQLstatement.ThismeansyoucandisplayyouroutputorsendittoalistfilewithoutspecifyingthePRINTprocedure.

TheORDERBYclausesortsdatabycolumns.Inaddition,tablesdonotneedtobepresortedbyavariableforusewithPROCSQL.Therefore,youdonotneedtousetheSORTprocedurewithyourPROCSQLprograms

APROCSQLstatementrunswhenyousubmitit;youdonothavetospecifyaRUNstatement.IfyoufollowaPROCSQLstatementwithaRUNstatement,theSASSystemignorestheRUNstatementandsubmitsthestatementsasusual.ThisprocedurealwaysstartswithaPROCSQLstatement,andendwithaQUIT(notrun;)statement.

1.6HowPROCSQLIsUnique

PROCSQLdiffersfrommostotherSASproceduresinseveralways:

∙UnlikeotherPROCstatements,manystatementsinPROCSQLarecomposedofclauses.

∙ThePROCSQLstepdoesnotrequireaRUNstatement.PROCSQLexecuteseachqueryautomatically.IfyouuseaRUNstatementwithaPROCSQLstep,SASignorestheRUNstatement,executesthestatementsasusual,andgeneratesthenoteshownbelowintheSASlog.

∙UnlikemanyotherSASprocedures,PROCSQLcontinuestorunafteryousubmitastep.WhenyousubmitaPROCSQLstepwithoutendingit,thestatuslinedisplaysthemessagePROCSQLrunning.

1.7GeneralSyntax

Asmentionedabove,thisprocedurealwaysstartswithaPROCSQLstatement,andendwithaQUIT(notrun;)statement.The*isoftenusedinSQLtomean“everythingavailable”orselectallofthevariablesinthetable.Theorderoftheclausesisnotflexible.SQLcodeobeystherulesoftheSASsystemregardingmaxlengthsofvariablenames,reservedwordsetal.

PROCSQL;

ALTERTABLEinstruction-alter;

CREATEinstruction-create;

DELETEinstruction-delete;

DROPinstruction-drop/view,table,indexfrom;

INSERTinstruction-insert;

RESET;

SELECTinstruction-select;

Distinctobject-item

INTOmacro-variable-specification

FROMfrom-list

WHEREsql-expression

GROUPBYgroup-by-item

HAVINGsql-expression

ORDERBY

UnlikeotherSASprocedures,theorderofclauseswithinaSELECTstatementinPROCSQLisimportant.Clausesmustappearintheordershownabove.

TodothisUsethisstatement

_________________________________________________________

Modify,add,ordropcolumnsALTERTABLE

EstablishaconnectionwithaDBMSCONNECT

CreateanindexonacolumnCREATEINDEX

CreateaPROCSQLtableCREATETABLE

CreateaPROCSQLviewCREATEVIEW

DeleterowsDELETE

DisplayadefinitionofatableorviewDESCRIBE

TerminatetheconnectionwithaDBMSDISCONNECT

Deletetables,views,orindexesDROP

SendaDBMS-specificnonquery

SQLstatementtoaDBMSEXECUTE

AddrowsINSERT

SelectandexecuterowsSELECT

QueryaDBMSCONNECTIONTO

ModifyvaluesUPDATE

_____________________________________________________________

2.GeneratereportsusingPROCSQLandtheappropriateoptions

Besidesusingdifferentstatementsyntax,anobviousdistinctionbetweenPROCPRINTandPROCSQListhataQUITstatementisspecifiedforthelatter,ratherthanaRUNstatement,toterminateprocessing.

2.1ThePROCSQLstatementillustratedbelowproducesadetail-orientedreport(similartoPROCPRINT)consistingofallobservationsandvariables.

datapeople;

inputpersoniddateagerace$gender$@@;

formatdatemmddyy10.;

informatdatemmddyy8.;

cards;

00109/18/8723CaucasM00201/23/9055AsianM

00306/05/9316HispanicF00411/14/9321Afr-AmM

00503/27/9533Afr-AmF00608/28/9645CaucasF

00702/01/9827CaucasM00804/20/8819CaucasM

00907/12/9224HispanicF01012/06/9430AsianF

;

procsqldouble;/*nodouble,feedback,noprint,INOBS=n,OUTOBS=n,number*/;

title'Thisistheentiresubjects';

select*frompeople;

quit;

TheSELECTstatementisthesimplestandmostcommonlyusedinSQL.

INOBS=nrestrictsthenumberofrows(observations)thatPROCSQLretrievesfromanysinglesource.

OUTOBS=nrestrictsthenumberofrows(observations)intheoutput.ItissimilartoOBS.

FEEDBACKoptioninthePROCSQLstatementwritestheexpandedlistofcolumnstotheSASlog.ItisadebuggingtoolthatletsyouseeexactlywhatisbeingsubmittedtotheSQLprocessor.

NOEXECoptiondoesnotexecutethisprocedure.

/*toselectuniquevaluesofoneormorecolumnsthedistinctkeywordisused*/;

procsql;

createtablepatidas

selectdistinctpatient

fromalec.vitals;

quit;

procsqlnoexec;

selectage,race

frompeople;

quit;

TheSELECTStatement

TheSELECTstatement,whichfollowsthePROCSQLstatement,retrievesanddisplaysdata.Itiscomposedofclauses,eachofwhichbeginswithakeywordandisfollowedbyoneormorecomponents.TheSELECTstatementinthefollowingsamplecodecontainsfourclauses:

therequiredclausesSELECTandFROM,andtheoptionalclausesWHEREandORDERBY.Theendofthestatementisindicatedbyasemicolon.

procsql;

|-selectempid,jobcode,salary,

|salary*.06asbonus

|----fromsasuser.payrollmaster

|----wheresalary<32000

|----orderbyjobcode;

quit;

APROCSQLqueryproducesaresultsetthatcanbeoutputasareport,atable,oraPROCSQLview.

TypeofOutput

PROCSQLStatements

report

SELECT

table 

CREATETABLEandSELECT

Selectcolumnsandcreatenewcolumns

datasumy;

inputIdNumberJobcode$Salary@@;

cards;

1845BCK259961673CK1254771834BCK268961387NF225028

1100BCK250041677BCK260071663PA3264521353NF125823

1704NF1254651981FA1224131634BCK268961389NF225028

1100GK1250041977BCK260121363PA3264881083PT123823

1965PT1274651154FA122613

;

procsql;

createtablesyas

selectIdNumber,Jobcode,Salary,

avg(salary)asAvgSalary,count(idnumber)asN,

sum(salary)astotal_sumformat=dollar10.2,freq(jobcode)

ascountfromsumy(obs=10)

groupbyjobcode;

quit;

ThekeywordFROMspecifiesthetable.

WHEREclausecanbesubsetdatabasedonaconditionintheSELECTstatement.

Tocreateanewtablefromtheresultsofaquery,useaCREATETABLEstatementthatincludesthekeywordASandtheclausesthatareusedinaPROCSQLquery.TheCREATETABLEstatementstoresyourqueryresultsinatableinsteadofdisplayingtheresultsasareport.

2.2ThePROCSQLstatementproducessummaryreport

/*Tocreateatablewithaddinganewvariables*/;

seeaboveexample.

/*INcondition*/;

sql-expressionIN(constant<,constant>…)

sql-expressionIN(query-expresssion)

procsql;

title'InformationforCertainEmployeesOnly';

selectIdNumber,Salary,Jobcode

fromsumy

whereidnumbernotin(1363,1387,1704);

quit;

*whereidnumbernotin(selectsalaryfromsumywheresalarygt26000);

/*IScondition*/;

ISNULLandISMISSINGarepredicatesthattestforamissingvalue.ISNULLandISMISSINGareusedintheWHERE,ON,

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

当前位置:首页 > 考试认证 > 交规考试

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2