/* Name-reversing function */ PROC FCMP outlib=work.myfcns.dev; FUNCTION ReverseName(name $) $40; /* Character arguments have a $ after their names. Numeric arguments do not. */ /* The last $40 indicates that the function returns a character expression of length 40. */ /* If $ is not specified, the function returns a numeric value. */ RETURN(catx(' ',scan(name,2,','),scan(name,1,','))); /* The part inside RETURN() specifies the value that is returned from the function. */ endsub; quit; DATA CTL2015more; FILENAME webpage3 URL 'http://people.stat.sc.edu/hitchcock/tennissinglesmore.txt'; INFILE webpage3 DLM='09'X; INPUT Name :$24. Gender $ City :$10. State $ Rating RatingDate MMDDYY10. RatingType $ MatchWins MatchLosses GameWins GameLosses; run; OPTIONS cmplib=work.myfcns; DATA CTL2015names; SET CTL2015more; NewName=ReverseName(Name); run; PROC PRINT data=CTL2015names; VAR NewName Rating; run; /* Name-combining function of two arguments */ PROC FCMP outlib=work.myfcns.dev; FUNCTION CombineName(firstname $, lastname $) $40; /* Character arguments have a $ after their names. Numeric arguments do not. */ /* The last $40 indicates that the function returns a character expression of length 40. */ /* If $ is not specified, the function returns a numeric value. */ RETURN(catx(' ',firstname,lastname)); /* The part inside RETURN() specifies the value that is returned from the function. */ endsub; quit; data usc2014; input last_name $15. @17 first_name $17. @35 College $; datalines; Al Atwa Abdulbakr Saud A. ENGR Aal-Said Basen ENGR O'Toole Meghan CAS Nicholson Mary C. CAS Grego John BUSI Al Tuubi Nizar J. MED Al-Sharif Ali ENGR ; run; OPTIONS cmplib=work.myfcns; DATA usc2014names; SET usc2014; FullName=CombineName(first_name, last_name); run; PROC PRINT data=usc2014names; VAR FullName College; run; /* Inverse Subroutine */ proc fcmp outlib=work.funcs.temp; subroutine inverse(in, inv) group="generic"; outargs inv; if in=0 then inv=.; else inv=1/in; endsub; options cmplib=work.funcs; data _null_; x = 5; call inverse(x, y); put x= y=; /* prints to log */ run; /* Win Percentage Subroutine */ proc fcmp outlib=work.funcs.temp; subroutine winpct(win, loss, pct) group="generic"; outargs pct; pct=100*(win/(win+loss)); endsub; options cmplib=work.funcs; data CTLpcts; SET CTL2015more; call winpct(matchwins, matchlosses, mwinpct); call winpct(gamewins, gamelosses, gwinpct); run; PROC SORT data=CTLpcts; BY DESCENDING mwinpct DESCENDING gwinpct ; run; PROC PRINT data=CTLpcts; VAR Name mwinpct gwinpct; run; /* Years until Turn 65 Subroutine */ proc fcmp outlib=work.funcs.temp; subroutine years2retire(birthday, current_date, years); outargs years; years = ((65*365.25) - (current_date - birthday))/365.25; endsub; options cmplib=work.funcs; data profs; input name :$9. bday MMDDYY10.; cards; Grego 10-18-1959 Hitchcock 11-01-1974 ; DATA profretire; SET profs; now=today(); how_long=.; call years2retire(bday, now, how_long); run; PROC PRINT data=profretire; FORMAT bday date10. now date10.; run;