SAS Tutorials
Home

Also... New
Quick Reference Guide for SAS

BeSmartNotes (tm)

Click for more info

New -- Kwikstat
Data Generator

Generate data sets with continuous, categorical, grouped and correlated data. Great for creating data sets for examples and for quality assurance testing. Data sets are created in standard .CSV format so they can be opened in most statistical programs such as SAS, SPSS, WINKS and into spreadsheet programs such as Microsoft Excel. Order now.

 

 

 

 SAS Functions
Tutorial and Reference, Part 2

See www.stattutorials.com/SASDATA for files mentioned in this tutorial © TexaSoft, 2008

  

How to use SAS Functions -- Part 2 (go to part 1)

SAS functions can be used in a DATA statement to calculate new variables or as a part of a conditional statement. This part includes information on the following types: Character, Date, Time, Truncation and Miscellaneous functions.

Not all SAS Functions are described in these tutorials and reference -- the most commonly used functions are discussed  — check SAS manuals for complete list.

For an example of how SAS functions work in a SAS program refer to part 1 of this tutorial.

Character Functions

  • COMPBL(text) - removes multiple blanks between words in a character string

  • COMPRESS(text<,characters-to-remove>) - removes specified characters from a character string

  • DEQUOTE(argument) - removes quotation marks from text

  • INDEX(text,excerpt) searches text for specified string — see also INDEXC and INDEXW

  • LEFT(text) - left-aligns a character string

  • LENGTH(text) - length of text

  • LOWCASE(text) - converts all letters to lowercase

  • QUOTE(text) - adds double quotation marks to text

  • REPEAT(text,n) repeats text n times

  • REVERSE(argument) - reverses a text expression

  • RIGHT(argument) - right-aligns a text

  • SCAN(argument,n<,delimiters>) -returns the nth word from a text expression. Example:

    • First=scan(“JOHN SMITH”,1,' '); returns “JOHN”

    • Last=scan(“JOHN SMITH”,2,' '); returns “SMITH”

  • SOUNDEX(text) - turns text into “sounds like” argument

    • soundex("ALLEN")=soundex("ALAN"); matches

    • soundex("ALLEN")=soundex("MELON"); does not

  • SUBSTR(text,start<,n>) Search and replace. Example:

    • var=SUBSTR(“LINCOLN”,3,2); returns var =“NC”

    • SUBSTR(“LINCOLN”,3,2)=“XX”; returns LIXX

  • TRANSLATE(text,to1,from1<,...ton,fromn>) - replaces characters in text with specified new characters

    • TRANSLATE('LINCOLN',"X","C"); returns LINXOLN

  • TRANWRD(source,target,replacement) replaces or removes all occurrences of a word in a text string

    • TRANWRD('SPECIAL WED MEAL','WED','MON');

    • Returns ‘SPECIAL MON MEAL’

  • TRIM(argument) - removes trailing blanks from text. (Returns one blank if the expression is missing.)

  • TRIMN(argument) - removes trailing blanks from text. (Returns a null string if the expression is missing.)

  • UPCASE(argument) - converts text to uppercase

  • VERIFY(source,excerpt-1<,...excerpt-n) - searches text, returns position of 1st non-matched character in 1st argument. Example:

    •  VERIFY(“12345”,”54301”) returns 2 since the 2nd character in “12345” does not match any character in 2nd argument.

Date and Time Functions

  • DATDIF(sdate,edate,basis) - num days between two dates. Basis is (default) '30/360','ACT/ CT', 'ACT/360', or 'ACT/365'
  • DATE() - current date (SAS date value)
  • DATEJUL(julian-date) - converts Julian date to SAS date
  • DATEPART(datetime) - extracts date from SAS datetime
  • DATETIME() - current date and time of day
  • DAY(date) - day of month from a SAS date
  • DHMS(date,hour,minute,second) - SAS datetime value from date, hour, minute, and second
  • HMS(hour,minute,second) - SAS time value from hour, minute, and second
  • HOUR(<time | datetime>) - hour from a SAS time or datetime value
  • INTCK('interval',from,to) - number of time intervals in a given time span. ‘Interval’ can be:
  • Date Intervals: DAY, WEEKDAY, WEEK, TENDAY, SEMIMONTH, MONTH, QTR, SEMIYEAR, YEAR
  • Datetime Intervals: DTDAY, DTWEEKDAY, DTWEEK, DTTENDAY, DTSEMIMONTH, DTMONTH, DTQTR, DTSEMIYEAR, DTYEAR
  • Time Intervals: HOUR, MINUTE, SECOND
  • INTNX('interval',start-from,increment<,'alignment'>) - advances a date, time, or datetime value by a given interval, and returns a date, time, or datetime value — see intervals above
  • JULDATE(date) - Julian date from a SAS date
  • MDY(month,day,year) - SAS date from month, day, year
  • MINUTE(time | datetime) - minute from a SAS time or datetime value
  • MONTH(date) - month from a SAS date value
  • QTR(date) - quarter of the year from a SAS date value
  • SECOND(time | datetime) - second from a SAS time or datetime value
  • TIME() - current time of day
  • TIMEPART(datetime) - time value from a SAS datetime
  • TODAY() - current date as a SAS date
  • WEEKDAY(date)- day of the week from a SAS date
  • YEAR(date) - year from a SAS date value
  • YRDIF(startdate,enddate,basis) - diff in years between 2 dates. Basis is '30/360','ACT/ CT', 'ACT/360', or 'ACT/365'
  • YYQ(year,quarter) - SAS date from year and quarter
  • Truncation Functions

    • CEIL(value) - smallest integer GE argument
    • FLOOR(value) - largest integer LE argument
    • FUZZ(value) - nearest integer if value is within 1E-12
    • INT(value) - decimal portion of the value of the argument
    • ROUND(value,roundoffunit) - value to the nearest roundoff unit
    • TRUNC(number, length) - truncated numeric value of a specified length

    Selected Misc Functions

    • IRR(period,cash0,cash1,...) internal rate of return as a percentage
    • MORT(amount,payment,rate,number) loan calculation. Example carloan MORT(30000,.,.06/12,48); (Payment set at .) returns $704.55 monthly payment.
    • NPV(rate,period,cash0,cash1,..) net present value expressed as
    • ZIPSTATE(zipcode) convert ZIP codes to two-letter state codes
    • ZIPNAMEL(zipcode) convert ZIP codes to state name (upper and lowercase)

     

    End of tutorial -- go back to part 1

     

    NOTE: SAS FUNCTIONS tutorial is available as a laminated reference card. Click here for more info

     

    See http://www.stattutorials.com/SAS

     

     

    numbers

    Get the SAS BeSmartNotes Quick Reference Guide

    Order


    | Send comments | Back to Tutorial Menu | TexaSoft |

    © Copyright TexaSoft, 1996-2007

    This site is not affiliated with SAS(r) or SAS Institute