+ All Categories
Home > Documents > 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data...

13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data...

Date post: 20-Jul-2018
Category:
Upload: vuongquynh
View: 213 times
Download: 0 times
Share this document with a friend
25
13. Trasforming data with SAS func)ons GIORGIO RUSSOLILLO - Cours de prépara)on à la cer)fica)on SAS «Base Programming» 305
Transcript
Page 1: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

13.TrasformingdatawithSASfunc)ons

GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming» 305

Page 2: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

SASfunc)ons

306GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

FUNCTION-NAME(argument1<,…,argument-n>);argumentscanbe:-  Variables

-  Ex.:MEAN(var1,var2,var3)-  Constants

-  Ex.:MEAN(456,12,5)-  Expressions

-  Ex:MEAN(456*2,12/5,5,MEAN(456,12,5))

-  Forsomefunc)onsvariableslistsandarrayscanalsobeusedasarguments,precededbythewordOF-  Ex.:MEAN(OFvar1-var3)MEAN(OFnewarray{*})

Page 3: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

TARGETVariables

307GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

Atargetvariableisthevariabletowhichtheresultofafunc)onisassigned-  Ex.:AvgVar=MEAN(var1,var2,var3);

-  Unlessthelengthofthetargetvariablehasbeenpreviouslydefined,adefaultlengthisassigned

-  Thedefaultlengthdependsonthefunc)on(forcharacterfunc)oncanbeaslongas200)

-  Defaultlengthcouldtakemorespacethannecessary.Tosavestoragespace,youcanaddaLENGTHstatementtospecifyalengthforthecharactertargetvariablebeforethestatementthatcreatesthevaluesofthatvariable

Page 4: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

Exampledataset

308GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

PROCSORTDATA=Lib9_3.Order_factOUT=Order_fact;BYCustomer_ID;WHEREyear(Order_Date)=2007;

RUN;DATAOrders(KEEP=Customer_IDCustomer_FirstNameCustomer_LastnameCustomer_NameGenderCustomer_addressStreet_numberBirth_DateDelivery_DateOrder_DateCostPrice_Per_Unit);

MERGELib9_3.Customer(IN=cust)Work.Order_fact(IN=order);BYCustomer_ID;IForder=1ANDcust=1ANDCustomer_ID<20;

RUN;PROCCONTENTSDATA=Orders;RUN;PROCPRINTDATA=Orders;RUN;

Page 5: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

Automa)ccharacter-to-numericconversion

309GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

-  Ifyoureferenceacharactervariableinanumericcontext(inanarithme)copera)on,inanumericcomparisonorinafunc)onasanargument),SAStriestoconvertthevariablevaluestonumeric.

-  SASdoesnotchangethevariabletype,butitcreatesatemporarynumericvalueforeachcharactervalueofthevariabletoconvert.

-  Whendataisautoma)callyconverted,amessageiswri{entotheSASlog-  TheWHEREstatementdoesnotperformautoma)cconversionincomparison

DATAprova(KEEP=NewvarOrder_Date);SETorders;Newvar=street_number+costprice_per_unit;Order_Date=Customer_FirstName;

RUN;PROCPRINTDATA=prova;RUN;

Theautoma)cconversion-  Usesthew.informat,wherewisthewidthofthecharactervaluethatisbeingconverted-  Producesanumericmissingvaluefromanycharacterthatdoesnotconformtostandard

numericnota)ons

Page 6: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

Explicitcharacter-to-numericexpression

310GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

INPUT(source,informat);-  Source:Indicatesthecharactervariable,constantorexpressiontobe

convertedtoanumericvalue-  Informat:anumericinformat

DATAprova(KEEP=NewvarNewvar2street_numbercostprice_per_unit);SETorders;Newvar=INPUT(street_number,4.)+costprice_per_unit;Newvar2=INPUT(street_number,3.)+costprice_per_unit;

RUN;PROCPRINTDATA=prova;RUN;

Thisfunc)onconvertsacharactercharactervariable,constantorexpressiontoanumericvalue

Page 7: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

Automa)cnumeric-to-characterconversion

311GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

-  Ifyoureferenceanumericvariableinacharactercontext(whenassigninganumericvaluetoacharacter-typevariable,whenusinganumericvaluewithanoperatorforcharactervaluesorinafunc)onwhichrequirescharactervalues),SAStriestoconvertthevariablevaluesintocharacters.

-  Whendataisautoma)callyconverted,amessageiswri{entotheSASlog

Theautoma)cconversion-  SASwritesthenumericvalueusingtheBEST12.format.Thisimpliesthatiftheoriginal

numericvaluehasfewerthan12digits,theresul)ngcharacterwillhaveleadingblanks

DATAprova(KEEP=Customer_LastName2Customer_LastNamecostprice_per_unit);SETorders;Customer_LastName2=Customer_LastName;Customer_LastName=costprice_per_unit;

RUN;PROCPRINTDATA=prova;RUN;PROCCONTENTSDATA=prova;RUN;

Page 8: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

Explicitnumeric-to-characterconversion

312GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

PUT(source,format);-  Sourceindicatesthenumericvariable,constantorexpressiontobe

convertedtoacharactervalue-  Format:aformatmatchingthedatatypeofthesource

N.B.:numericformatsright-aligntheresult;characterformatsle|-aligntheresults(SeevariablesStreet_NumberandCostPrice_per_Unit)

Thisfunc)onconvertsanumericvariable,constantorexpressiontoacharacterstring

DATAprova(KEEP=Customer_LastNamecostprice_per_unitnewCP_unitStreet_Number);SETorders;Customer_LastName=costprice_per_unit;newCP_unit=PUT(costprice_per_unit,DOLLAR7.2);

RUN;PROCPRINTDATA=prova;RUN;

Page 9: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

Manipula)ngSASdatevalueswithfunc)ons

313GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

SASdate,)meanddate)mevariablesarenumericvariablesSASstores:-  AdatevalueasthenumberofdaysfromJanuary1,1960toagivendate

-  A)mevalueasthenumberofsecondssincemidnight

-  Adate)mevalueasthenumberofsecondsfrommidnightonJanuary1,1960toagivendateand)me

Page 10: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

YEAR,QRT,MONTHandDAYfunc)ons

314GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

YEAR(date)QTR(date)MONTH(date)DAY(date)WEEKDAY(date)

-DateisaSASdatevaluethatisspecifiedeitherasavariablesorasaSASdateconstant

DATAprova(KEEP=Birth_DateWeekDayDayMonthYear);SETorders;WeekDay=WEEKDAY(Birth_Date);Day=DAY(Birth_Date);Month=MONTH(Birth_Date);Year=YEAR(Birth_Date);

RUN;PROCPRINTDATA=prova;RUN;

-  YEAR returns a four-digit numeric value thatrepresentstheyear

-  QTRreturnsavalueof1,2,3or4toindicatethequarteroftheyearinwhichthedatevaluefalls

-  MONTH returns a value from 1 to 12represen)ngthemonth

-  DAY returns a numeric value from 1 to 31represen)ngthedayofthemonth

-  WEEKDAY returns a numeric value from 1(sunday)to7(saturday)represen)ngthedayoftheweek

Page 11: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

MDYfunc)on

315GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

MDY(month,day,year)-  month:anumberfrom1-12oravariablerepresen)ngthemonth-  day:anumberfrom1-31oravariablerepresen)ngtheday-  year:anumberthathas2or4digitsoravariablerepresen)ngtheyear.

DATAprova(KEEP=Birth_DateWeekDayDayMonthYearmyDATE);SETorders;Day=DAY(Birth_Date);Month=MONTH(Birth_Date);Year=YEAR(Birth_Date);myDATE=MDY(Month,Day,Year);

RUN;PROCPRINTDATA=prova;

FORMATmyDateDATE9.;RUN;

-  Ifyouspecifyaninvaliddate,SASreturnsamissingdata-  Ifyouspecifyonly2digitspaya{en)ontotheYEARCUTOFFsystemop)on!

Thisfunc)onreturnstheSASdatevaluecorrespondingtotheday,monthandyearspecifiedinthearguments

Page 12: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

DATEandTODAYfunc)ons

316GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

DATE()TODAY()

-Thesefunc)onsdonotrequireanyarguments,buttheymusts)llbefollowedbyparenthesis

-  Thesefunc)onsreturnthecurrentdatefromthesystemclockasaSASdatevalue-  Theycanusedinterchangeably

DATAprova(KEEP=Today);SETorders;Today=DATE();

RUN;PROCPRINTDATA=prova(OBS=5);

FORMATTodayDATE9.;RUN;

Page 13: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

INTCKfunc)on

317GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

INTCK(‘interval’,from,to)-  ‘interval’specifiesacharacterconstantorvariable.Itcanoneamong:DAY,WEEKDAY,WEEK,TENDAY,

SEMIMONTH,MONTH,QTR,SEMIYEAR,YEAR.Thetypeofinterval()me,date,date)me)mustmatchthetypeoffrom

-  fromspecifiesaSASdate,)meordate)mevaluethatindicatesthebeginningofthe)me-  tospecifiesaSASdate,)meordate)mevaluethatindicatestheendofthe)mespan

Forexample,WEEKintervalsarecountedbySundays,MONTHintervalsarecountedfromtheday1ofeachmonthandYEARintervalsarecountedfromJAN01.

DATAprova(KEEP=Birth_DateOrder_DateDelivery_DateDelai_LivrSem1Sem2MoisAnAge);SETorders;Delai_Livr=INTCK("DAY",Order_Date,Delivery_Date);Sem1=INTCK("WEEK",'31dec2011'd,'01jan2012'd);Sem2=INTCK("WEEK",'31dec2012'd,'01jan2013'd);Mois=INTCK("MONTH",'31dec2012'd,'01jan2013'd);An=INTCK("YEAR",'31dec2012'd,'01jan2013'd);Age=INTCK("YEAR",Birth_date,TODAY());

RUN;PROCPRINTDATA=prova;RUN;

Thisfunc)oncountsintervalsfromfixedintervalbeginnings

Page 14: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

TheINTNXfunc)on

318GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

TheINTNXfunc)onappliesmul)plesofagivenintervaltoadate,)meordate)mevalueandreturnstheresul)ngvalue

INTNX(‘interval’,start-from,increment,<‘alignement’>)-  ‘interval’ specifiesa character constantof variable. It canoneamong:DAY,WEEKDAY,WEEK,TENDAY,

SEMIMONTH,MONTH,QTR,SEMIYEAR,YEAR.Thetypeofintervalmustmatchthetypeofstart-from-  start-fromspecifiesaSASdate,)meordate)mevaluethatindicatesthebeginningofthe)me-  incrementnega)veorposi)veintegerthatrepresents)meintervalstowardthepastorthefuture-  ‘alignment’specifiesthatthereturneddateisalignedtothebeginning(BEGINNINGorB,defaultvalue),

middle(MIDDLEorM),end(ENDorE)oftheinterval,orthesameday(SAMEDAYorS)oftheinputdate

DATAprova(KEEP=Order_DateIntervMonthBIntervMonthMIntervMonthEIntervMonthS);SETorders;IntervMonthB=INTNX('MONTH',Order_Date,2,'B');IntervMonthM=INTNX('MONTH',Order_Date,2,'M');IntervMonthE=INTNX('MONTH',Order_Date,2,'E');IntervMonthS=INTNX('MONTH',Order_Date,2,'S');

RUN;PROCPRINTDATA=prova(OBS=10);

FORMATIntervMonthBIntervMonthMIntervMonthEIntervMonthSdate9.;RUN;

Page 15: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

DATDIFandYRDIF

319GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

DATDIF(start-date,end-date,‘basis’)YRDIF(start-date,end-date,‘basis’)

-  start-datespecifiesthestar)ngdateasaSASdatevalue-  end-datespecifiestheendingdateasaSASdatevalue-  basisspecifieshowSAScalculatethedatedifference

•  FourcharacterstringsarevalidforbasisinYRDIFfunc)on:•  ’30/360’:specifiesa30daymonthanda360dayyear•  ‘ACT/ACT’:usestheactualnumberofdaysoryearsbetweendates•  ‘ACT/360’:usestheactualnumberofdaysbetweendatesanddividesitby360forcalcula)ngthenumber

ofyears(validonlyforYRDIF)•  ‘ACT/365’:usestheactualnumberofdaysbetweendatesanddividesitby365forcalcula)ngthenumber

ofyears(validonlyforYRDIF)

Theyreturnthedifferenceindaysandyearsbetweentwodates,respec)vely

DATAprova(KEEP=Birth_DateOrder_DateDelivery_DateDelai_LivrDelai_Livr1AgeAge1);SETorders;Delai_Livr=DATDIF(Order_Date,Delivery_Date,"ACT/ACT");Delai_Livr1=INTCK("DAY",Order_Date,Delivery_Date);Age=YRDIF(Birth_date,TODAY(),"ACT/ACT");Age1=INTCK("YEAR",Birth_date,TODAY());

RUN;PROCPRINTDATA=prova;RUN;

Page 16: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

SCANfunc)on

320GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

SCAN(argument,n,<delimiters>)-  argumentspecifiesthecharactervariableorexpressiontoscan-  nspecifieswhichwordtoreturn-  delimitersarespecialcharactersthatmustbeenclosedinsinglequota)onmarks

DATAprova(KEEP=Customer_NameNomPrenom);SETorders;LENGTHNom$20Prenom$20;Nom=SCAN(Customer_Name,2);Prenom=SCAN(Customer_Name,1);

RUN;PROCPRINTDATA=prova;RUN;

-  Ifyouspecifymul)pledelimiters,SASusesanyofthedelimiters,singularlyoninanycombina)on

-  Defaultdelimiters:blank.<(+&!$*);^-/,%

Thisfunc)onseparatesacharactervalueintowordsandreturnsthen-thword

N.B.:This func)onassignsa lengthof200 toeach targetvariable. To save storage space, you can add aLENGTH statement to specify a length for thecharacter target variablebefore the statement thatcontainstheSCANfunc)on

Page 17: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

SUBSTRfunc)on

321GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

Thisfunc)onextractsorreplaceapor)onofacharactervariable-  Whenthefunc)onisontherightsideofanassignmentstatement,thefunc)on

returnstherequestedstring-  Whenthefunc)onisonthele|sideofanassignmentstatement,thefunc)on

replacesthestringbythestringindicatedontherightside

SUBSTR(argument,posi+on,<n>)-  argumentspecifiesthecharactervariableorexpressiontoscan-  posi+onisthecharacterposi)ontostartfrom-  nspecifiesthenumberofcharactertoextract.Ifitisomi{edalltheremaining

charactersareincludedinthesubstring

DATAprova(KEEP=Customer_NameIni)al);SETorders;Ini)al=SUBSTR(Customer_Name,1,1);IF(Customer_NameEQ'SandrinaStephano')THENSUBSTR(Customer_Name,6,3)='ona';

RUN;PROCPRINTDATA=prova(OBS=7);RUN;

Page 18: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

TRIMfunc)on

322GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

Thisfunc)onremovestrailingblanksfromcharactervalues

TRIM(argument)-  argumentspecifiesanycharacterexpression

Page 19: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

CATXfunc)on

323GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

CATX(separator,string-1<,…,string-n>)-  separatorspecifiesthecharacterstringthatisusedasa

separatorbetweenconcatenatedstrings-  stringspecifiesaSAScharacterstring

Thisfunc)onconcatenatescharacterstrings,removesleadingandtrailingblanksandinsertseparators

DATAprova(KEEP=Customer_FirstNameCustomer_LastNamemyNameGender);SETorders;IF(GenderEQ'F') THENmyName=CATX('','M.me',Customer_FirstName,Customer_LastName); ELSEmyName=CATX('','M.',Customer_FirstName,Customer_LastName);

RUN;PROCPRINTDATA=prova;RUN;

Page 20: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

INDEXfunc)on

324GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

INDEX(source,‘excerpt’)-  sourcespecifiesthecharactervariableorexpressiontosearch-  excerptspecifiesacharacterstringinclosedinquota)onmarks

Thisfunc)onfindsthestringandreturnstheposi)onofthestring’sfirstcharacter;ifitisnotfound,itreturnsavalueof0

DATAprova(KEEP=Customer_NameBlack_pos);SETorders;Black_pos=INDEX(Customer_Name,'Black');IFBlack_pos>0;

RUN;PROCPRINTDATA=prova;RUN;

Page 21: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

FINDfunc)on

325GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

FIND(string,substring<’modifiers’><startpos>)-  stringspecifiesthecharacterconstant,variableorexpressionthatwillbesearchedforsubstrings-  substringisthecharacterconstant,variableorexpressionthatspecifiesthesubstringofcharacters

tosearchforinstring-  modifiersisthecharacterconstant,variableorexpressionthatspecifiesoneormoremodifiers.-  startposisanintegerthatspecifiestheposi)onatwhichtheresearchshouldstart.Itssignspecifies

thedirec)onofthesearch(-=le|,+=right)

Thisfunc)onfindsthesubstringandreturnstheposi)onofthesubstring’sfirstcharacter;ifitisnotfound,itreturnsavalueof0.FIND issimilar to INDEX,but itallows ‘modifiers’anda ‘star)ngposi)on’ tobespecified(between quota)onmarks). Twomodifiers exist, ‘i’ and ‘t’. The ‘i’modifier tells SAS toignorethecase,the‘t’modifiertrimstrailingblanksfromstringandsubstring

DATAprova(KEEP=Customer_Address);SETorders;IFFIND(Customer_Address,'1068','t',-6)>0ORFIND(Customer_Address,'bryant',"i",1)>0;RUN;PROCPRINTDATA=prova;RUN;

DATAprova(KEEP=Customer_Address);SETorders;IFFIND(Customer_Address,'1068',-6)>0;

RUN;PROCPRINTDATA=prova;RUN;

Page 22: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

UPCASEandLOWCASE

326GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

UPCASE(argument)LOWCASE(argument)

-  argumentcanbeanySASexpression

Thisfunc)onsconvertallle{ersinacharacterexpressontouppercaseandlowecase,respec)vely

DATAprova(KEEP=Customer_LastName);SETorders;Customer_LastName=UPCASE(Customer_LastName);

RUN;PROCPRINTDATA=prova;RUN;

Page 23: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

PROPCASE

327GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

PROPCASE(argument,<delimiter(s)>)-  argumentcanbeanySASexpression-  delimitersspecifiesoneormoredelimiterseclosedinquota)onmarks.

Thedefaultdelimitersare:blank/-(.Tab

Thisfunc)onconvertscharacterexpressionssothatthefirstle{erineachwordiscapitalised

DATAprova(KEEP=Customer_NameC_N);SETorders;Customer_Name=UPCASE(Customer_Name);C_N=PROPCASE(Customer_Name);

RUN;PROCPRINTDATA=prova;RUN;

Page 24: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

TRANWRDfunc)on

328GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

TRANWRD(source,target,replacement)-  sourcespecifiesthesourcestringthatyouwanttotranslate-  targetspecifiesthestringthatSASsearchesfor-  replacementspecifiesthestringthatreplacestarget

Thisfunc)onremovesalloccurrencesofapa{ernofcharactersfromthesourcestringandreplacesthembyanewsubstring

DATAprova(KEEP=Customer_NameCustomer_Name2);SETorders;Customer_Name2=TRANWRD(Customer_Name,'Sandrina','Laura');

RUN;PROCPRINTDATA=prova(OBS=7);RUN;

Page 25: 13. Trasforming data with SAS funconsmaths.cnam.fr/IMG/pdf/SAS_13_cle43b72e.pdf · Trasforming data with SAS funcons GIORGIO RUSSOLILLO - Cours de préparaon à la cer) ... - day

Modifyingnumericvalueswithfunc)ons

329GIORGIORUSSOLILLO-Coursdeprépara)onàlacer)fica)onSAS«BaseProgramming»

INT(argument)-  argumentcanbeanumericvariable,constantorexpression

ROUND(argument,round-off-unit)-  argumentcanbeanumericvariable,constantorexpression-  round-off-unitisnumericandnon-nega)ve.Itmustbeprecededbyaperiod

Returnstheintegerpor)onofanumericvalue

Roundsvaluestonearestspecifiedunit

DATAprova(KEEP=CostPrice_Per_UnitRoundedPriceIntegerPrice);SETorders;RoundedPrice=ROUND(CostPrice_Per_Unit,.1);IntegerPrice=INT(CostPrice_Per_Unit);

RUN;PROCPRINTDATA=prova;

FORMATRoundedPriceIntegerPriceDOLLAR7.2;RUN;


Recommended