What's function?
In wiki:
In mathematics, a function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output.
What about SAS functions?

<
As you can see, SAS 9.2 now has nearly 500 functions available. And SAS 9.2 brings one of the greatest improvement---user-defined functions.
It's no need to say how important to learn the SAS functions if you want to improve your sas programming.
SAS/BASE provides two types of powerful functions (from SAS.com) :
1. Function
"A
SAS function performs a computation or system manipulation on arguments, and returns a value that can be used in an assignment statement or elsewhere in expressions.
In Base SAS software, you can use SAS functions in DATA step programming statements, in a WHERE expression, in macro language statements, in PROC REPORT, and in Structured Query Language (SQL).
Some statistical procedures also use SAS functions. In addition, some other SAS software products offer functions that you can use in the DATA step. Refer to the documentation that pertains to the specific SAS software product for additional information about these functions."
Examples:
-
x=max(cash,credit);
-
x=sqrt(1500);
-
NewCity=left(upcase(City));
-
x=min(YearTemperature-July,YearTemperature-Dec);
-
s=repeat('----+',16);
-
x=min((enroll-drop),(enroll-fail));
-
dollars=int(cash);
-
if sum(cash,credit)>1000 then
put 'Goal reached';
2. Call routine
"A
CALL routine alters variable values or performs other system functions. CALL routines are similar to functions, but differ from functions in that you cannot use them in assignment statements or expressions.
All SAS CALL routines are invoked with CALL statements. That is, the name of the routine must appear after the keyword CALL in the CALL statement."
Examples:
-
call prxsubstr(prx,string,position);
-
call prxchange('/old/new',1+k,trim(string),result,length);
-
call set(dsid);
-
call ranbin(Seed_1,n,p,X1);
-
call label(abc{j},lab);
-
call cats(result,'abc',123);
Learn by Example
Suppose you need to import and process a messy txt file "Demog":

And do the following:
1. find the last name from the name
2. put (Year) after the Age
3. extract the number of weight from the WT (eg, 155lbs)
4. keep the Quote of each person with the quotation mark
5. order the scores and calculate the mean of the top three scores
6. replace the "XXX" in the "Demog" with "Unknown"
Target
Code
/* macro variable to store the path */
%let path=C:\Documents and Settings\Administrator\My Documents\Dropbox\Transfer\tech post\dlmstr;
data raw;
/* Define the sequence of the variables and length */
length Name $20. Last_name $10. Gender $7. Age 3. Age_Year $15.
Height 3. WT $6. WT_Lbs Position $15. Quote $20.
score1-score5 3.;
/* Options: truncover & dlmstr & dsd */
infile "&path\demog.txt" truncover dlmstr="@#" dsd;
input @;
/* Perl regular expressions */
_infile_ = prxchange("s/XXX/Unknown/",-1,_infile_);
input Name $
Gender $
Age
Height
WT $
Position $
/* use ~ to keep quotation mark; */
Quote ~ $
score1-score5;
/* put */
Age_Year = put(Age, 3.)||" (Years)";
/* compress */
WT_Lbs = input( compress(WT, , "kd"), 3.);
/* propcase */
Last_name = propcase( scan(Name, -1, " ") );
/* call & sortn */
call sortn(of score1-score5);
/* round & mean */
top3 = round(mean(of score3-score5), 0.1);
run;
Discussion
Lots of things need to take a close look:
- infile method to import txt flat file & options: truncover, dlmstr, dsd
- Perl regular expressions : prxchange
- automatic variable : _infile_
- compress
- propcase
- scan
- sortn
- call routine