0% found this document useful (0 votes)
4 views

3 Functions

The document provides an overview of various functions in SAS for data manipulation, including TRANWRD, UPCASE, SUBSTR, SCAN, and others. It explains how to replace, modify, and format character strings, as well as perform mathematical operations like rounding and calculating means. Additionally, it covers the use of INPUT and PUT functions for converting between character and numeric values.

Uploaded by

Arijit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

3 Functions

The document provides an overview of various functions in SAS for data manipulation, including TRANWRD, UPCASE, SUBSTR, SCAN, and others. It explains how to replace, modify, and format character strings, as well as perform mathematical operations like rounding and calculating means. Additionally, it covers the use of INPUT and PUT functions for converting between character and numeric values.

Uploaded by

Arijit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

FUNCTIONS

TRANWRD
• TRANWRD FUNCTION IS USED REPLACES ALL OCCURENCES
OF A
• * PATTERN OF CHARACTERS IN A DATA VALUE WITH
ANOTHER PATTERN OF CHARACTERS.

• * var1=tranwrd(var ,target,replacement);

• DATA Radhika;
• INPUT NAME $20.;
• NEW=TRANWRD(NAME,'MISS','MS');
• CARDS;
• MISS.SIMRAN
• MISS.SOUNDARYA
• MISS.AARTHI
• ;
• RUN;
• PROC PRINT; RUN;
TRANWARD
UPCASE
• DATA Radhika;
• INPUT NAME $20.;
• NEW=upcase(name);
• CARDS;
• Radhika
• Suresh
• aarthi
• ;
• RUN;
• PROC PRINT;
• RUN;
SUBSTR
• Replaces character value contents:

• DATA Radhika;
• INPUT NAME $20.;
• NEW=SUBSTR(name,4,3);
• CARDS;
• Radhika Kulkarni
• Suresh BHARATHA
• aarthi KULKARNI
• ;
• RUN;
• PROC PRINT;
• RUN;
SUBSTR
SCAN
• Returns a given word from a character
expression
• DATA Radhika;
• INFILE CARDS ;
• INPUT NAME $30.;
• NEW=SCAN(NAME,2);
• CARDS;
• Radhika Kulkarni
• SURESH BHARATHA
• BABU MANNE
• NAVIN VANGIPURAPU Neha
• ;
• RUN;
• PROC PRINT; RUN;
SCAN O/P FROM PREV PR
HOW DO U GET THIS SCAN
SCAN – OTHER EXAMPLE
• DATA Radhika;
• INFILE CARDS ;
• INPUT NAME $30.;
• NEW=SCAN(NAME,2,'&');
• CARDS;
• Radhika Kulkarni
• Suresh Bharatha &Janni
• BABU MANNE
• NAVIN VANGIPURAPU &Neha
• ;
• RUN;

• PROC PRINT;
• RUN;
SCAN – OTHER EXAMPLE
Concatenation Operator
• The concatenation operator joins character data values together
• Var2 = Var1 || var2 || … || var(n).
• If created variables was not properly defined with a Length statement
, the length of a new variable is the sum of the lengths of all variables
and constant text being concatenated.
• TRIM function
• The TRIM function is used to remove trailing blanks from character
expression and returns one blank if the expression is missing during
execution.
• Var2= TRIM(var1) || var2; Trim by itself has visible effect on a
result.
• Example
• Name1=Trim(name);
• If you assume the NAME has a length of 9, what is the length of
NAME1 ? When is the length assigned , at Compilation or
Execution ?
• Name1 also has a length of 9. During execution, the
value for NAME is trimmed, but when it is placed in the
slot in the PDV for NAME1, extra blanks are added to
pad it out to a length of 9.
Ex:- Concatenation Operator
• DATA Radhika;
• INFILE CARDS ;
• INPUT FirstName $ LastName $;
• CONCOT=FirstName || LastName;
• TRIM_CONCOT = Trim(FirstName) || LastName;

• LIST;
• CARDS;
• Radhika Kulkarni
• Satya Pala
• Suresh Bharatha
• Sairam Yeruva
• ;
• RUN;

• PROC PRINT;
• RUN;
Ex:- Concatenation Operator
Another Ex: TRIM & CONCAT
• data test;
• input part1 $ 1-10 part2 $ 11-20;
• hasblank=part1||part2;
• noblank=trim(part1)||part2;
• put hasblank;
• put noblank;
• datalines;
• apple sauce
• ;
• RUN;
• Proc Print;
• run;
Another Ex: TRIM & CONCAT
TRIM – ANOTHER EXAMPLE
• data test;
• x="A"||trim(" ")||"B";
• x1=" ";
• y=">"||trim(x1)||"<";
• Run;

• Proc Print;
• run;
• Note: Regardless of how much space you
provide for X1, TRIM will cut into One
Space only.In case if you use TRIMN it will
cut to ZERO Space…..Try the same using
TRIMN…
TRIM – ANOTHER EXAMPLE
REVERSE
• REVERSE
• DATA AMOUNTS;
• INPUT @1 CHARAMT $8.;
• SIGN=REVERSE(CHARAMT);
• DATALINES;
• RADHIKA
• RAJARAM
• SURESH
• SATYAKALYANI
• ;
• RUN;
• PROC PRINT;
• run;
LEFT
• DATA AMOUNTS;
• INPUT @1 CHARAMT $9.;
• SIGN=LEFT(CHARAMT);
• DATALINES;
• Radhika
• RAJARAM
• RAMANA
• AARTHI
• ;
• RUN;
• PROC PRINT;
• run;
LEFT
• DATA AMOUNTS;
• INPUT @1 CHARAMT $9.;
• SIGN=LEFT(CHARAMT);
• DATALINES;
• Radhika
• RAJARAM
• RAMANA
• AARTHI
• ;
• RUN;
• PROC PRINT;
• run;
COMPRESS
• COMPRESS: It will compress whatever is mentioned in the
statement. Here in this example it eliminates ‘-‘ in the
occurrence of the variable Charamt.
• DATA AMOUNTS;
• INPUT @1 CHARAMT $15.;
• SIGN=COMPRESS(CHARAMT,'-');
• DATALINES;
• RADHIKA
• RAJARAM
• SURESH
• AARTHI
• 919-608-7123
• ;
• RUN;
• PROC PRINT; run;
COMPRESS
TRANSLATE
TRANSLATE(source,to-1,from-1 <...to-n,from-
n>)
• source
• specifies the SAS expression containing the
original character value.
• to
• specifies the characters you want TRANSLATE to
use as substitutes.
• from
• specifies the characters you want TRANSLATE to
replace.
• TRANSLATE: (String, ‘what to PUT’,
‘what to Replace’);
TRANSLATE
• DATA Translate;
• INPUT @1 Input_String $12.;
• New_String=TRANSLATE(Input_String, '/', '-');
• DATALINES;
• 919-765-7206
• 919-308-9328
• 919-484-1967
• ;
• RUN;
• PROC PRINT;
• Run;
TRANSLATE
FLOOR
Returns the largest integer that is less
than or equal to the argument. Floor
function simply rounds a real number
down to the nearest integer.

To convert completed months to


completed years one uses

years=floor(months/12);
FLOOR
Returns the largest integer <= argument

Data floor;
a=floor(4.93); a1=floor(4.2);
a2=floor(2.28); a3=floor(0.91);
a4=floor(456.92); a5=floor(-1.6);
Run;
Proc print;
Run;
FLOOR
CEIL
• CEIL: Returns the smallest integer >=
argument
• The argument must be a numeric value.
• Data ceil;
• a=ceil(4.93); a1=ceil(4.2);
• a2=ceil(2.28); a3=ceil(0.91);
• a4=ceil(456.92); a5=ceil(-1.6);
• Run;
• Proc print;
• Run;
CEIL
INT
• INT: returns the integer value (truncates).
• This functions truncates the decimal portion
of the value of the argument. The integer
portion of the value of the argument remains.
– Data RADHIKA;
– X=int(2.1); x1=int(-2.4); x2=int(3); x3=int(-1.6);
– Run;
– Proc print;
– Obs X X1 X2 X3
– 1 2 -2 3 -1
MEAN
• MEAN: Computes the arithmetic mean (average)
• Mean(Argument,argument,……….ArgumentN);
• The argument for this function must be numeric
values. One or more non missing values are
required. The function results in the average of
the values of the non missing arguments.
• Example:
• Data RADHIKA;
• X=Mean(2,.,.,6) ; X1=Mean(1,2,3,2);
• Run;
• Proc Print; Run;
• Obs X X1
• 1 4 2
MIN
• MIN: Returns the smallest value. The argument for this
function must be numeric values. Two or more non
missing values are required. The function result is the
smallest values of the argument average of the values of
the non missing arguments. Min(argument,argument,…);

• Data RADHIKA;
• x=min(2,.,6);
• x1=min(2,-3,1,-1);
• x2=min(0,4);
• Run;
• Proc print;
• Run;

• Partial Output
• Obs X X1 X2
• 1 2 -3 0
MOD
• MOD: Calculates the remainder:
• The result of this function is the remainder when the
quotient of argument1 divided by the argument2 is
calculated.

• Data RADHIKA;
• x=Mod(6,3);
• x1=Mod(10,3);
• x2=Mod(11,3.5);
• x3=Mod(10,-3);
• Run;
• Proc print;
• run;

• Partial Output :
• Obs x x1 x2 x3
• 1 0 1 0.5 1
N
Reports the number of non missing
arguments.
• N(Argument,argument,…) The argument for this
function must be numeric values. At least one
argument is required. The functions returns the
no of non missing values.
• Data RADHIKA;
• X=N(1,0,.,2,5,.);
• Run;
• Proc print;
• Run;

• Partial Output
• Obs X
• 1 4
NMISS
• NMISS: Reports the number of missing
values .
• NMISS(Argument,Argument,….)

• Data RADHIKA;
• X=NMISS(1,0,.,2,5,.);
• Run;
• Proc print; Run;

• Obs X
• 1 2
ROUND
• ROUND: Rounds a value to nearest round off
unit. Round(Argument, Roundoffunit)

• Data RADHIKA;
• X=round(223.456,1);
• x1=round(223.456,.01);
• x2=round(223.456,100);
• X3=round(223.456);
• X4=round(-222.456,2);
• Run;
• Proc Print;
• Run;
ROUND
INPUT function
To convert CHARACTER values to NUMERIC
values you use the INPUT function .
INPUT(source,informat)

DATA CHAR_NUMERIC;
Length Numeric 3;
Char = '1234';
Numeric =Input(Char,4.);
Run;
Proc Print; Run;
Proc Contents Data=CHAR_NUMERIC;
RUN;
Data temp; INPUT function
Length Char_Var $4;
Input Numeric_Var Char_Var $;
/* convert character to Numeric_Var */
New_num=input(Char_Var,best4.);
/* convert Numeric_Var to character */
New_char=put(Numeric_Var,4.0);
cards;
789 1234
009 0009
1 9999
;
RUN;
PROC PRINT;
Var Char_Var New_num Numeric_Var New_Char;
run;
PUT function
To convert NUMERIC values to CHARACTER
values you use the PUT function.
PUT(source, format)
DATA CHAR_NUMERIC;
Numeric=1234;
Char=put(Numeric, $4.);
Run;
Proc Print; Run;
Proc Contents Data=CHAR_NUMERIC;
RUN;
INPUT & PUT
Data temp;
Length Char_Var $4;
Input Numeric_Var Char_Var;
/* convert character to Numeric_Var */
New_num=input(Char_Var,best4.);
/* convert Numeric_Var to character */
New_char=put(Numeric_Var,4.0);
cards;
789 1234
009 0009
1 9999
;
RUN;
PROC PRINT;
Var Char_Var New_num Numeric_Var New_Char;
run;

You might also like