Monday, 17 March 2014

SAS_Functions

Q)What is the difference between SAS function and procedures?
A SAS function return a single value while SAS call routine/procedure don’t return value.
A CALL routine, unlike a function,cannot be used in an assignment statement.

Example:Missing function Vs Call Missing routine
if missing(Gender) then MissGender + 1;
call missing(of X1–X10);

Q)How would you determine the number of missing or nonmissing values in computations?
 A)To determine the number of missing values that are excluded in a computation, use the NMISS function.
data _null_;
m = . ;
y = 4 ;
z = 0 ;
N = N(m , y, z);
NMISS = NMISS (m , y, z);
run;
The above program results in N = 2 (Number of non missing values) and NMISS = 1 (number of missing values).

Q)What
would be the denominator value used by mean function if 2 out of 7 arguments are missing?
A)five

Q)Briefly explain Input and Put function?
Q)How do i convert a numeric variable to a character variable?
Q)How do i convert a character variable to a numeric variable?


Input function – Character to numeric conversion- Input(source,informat)
put function –  Numeric to character conversion- put(source,format)

Q)Difference between COMPBL and COMPRESS?
COMPBL (compress blanks) can replace multiple blanks with a single blank. 
The COMPRESS function can remove not only blanks, but also any characters you specify from a string. 

Q)What is the use of ANYALNUM() ?
To locate the first occurrence of an alphanumeric character (any upper- or lowercase letter or number) and return its position.  If none is found, the function returns a 0.  With the use of an optional parameter, this function can begin searching at any position in the string and can also search from right to left, if desired
Examples  For these examples, STRING = "ABC 123 ?xyz_n_" 
ANYALNUM(STRING,5) 5 (the position of "1")
ANYALNUM(STRING,-4) 3 (the position of "C")
ANYALNUM(STRING,6) 6 (the position of "2")  .

Q)Difference between FIND and INDEX function?
FIND and INDEX both search a string for a given substring.  FINDC and INDEXC both search for individual characters.  However, both FIND and FINDC have some additional capability over their counterparts.  For example, this pair of functions has the ability to declare a starting position for the search, the direction of the search, and to ignore case or trailing blanks.

Q)How to use SUBSTR on right hand side and left hand side of = sign.
SUBSTR: extracts a sub string and replaces character values.
Extraction of a substring: Middleinitial=substr(middlename,1,1);
Replacing character values: substr (phone,1,3)=’433’; If SUBSTR function is on the left side of a statement, the function replaces the contents of the character variable.

 Q)What are the parameters of Scan function?
scan(argument,n,delimiters)
argument specifies the character variable or expression to scan
n specifies which word to read
delimiters are special characters that must be enclosed in single quotation marks
If n is greater than the number of words, the SCAN function returns a value that contains no characters. If n is negative, the character value is scanned from right to left. A value of zerois invalid.

Q)Suppose the variable address stores the following expression:
209 RADCLIFFE ROAD, CENTER CITY, NY, 92716
What would be the result returned by the scan function in the following cases?
a=scan(address,3);
b=scan(address,3,’,');
A)a=Road; b=NY

Q)What is the length assigned to the target variable by the scan function?
A)200

Q)When looking for data contained in a character string of 150 bytes, which function is the best to locate that data: scan, index, or indexc?
SCAN

Q)SCAN vs. SUBSTR:
SCAN extracts words within a value that is marked by delimiters.
SUBSTR extracts a portion of the value by stating the specific location.

Q)Difference between translate and transwrd function.
TRANSLATE can substitute one character for another in a string.  TRANWRD is more flexible—it can substitute a word or several words for one or more words. 

Q)If you have to find digits in a alphanumeric string..which function can be used
A)
Anydigit(); - returns the position of first digit in the given string

Q)If you have to mask last 5 digits of 10 char telephone no....which function can be used ?
X=10 digit telephone no = 9973523398 , Y=*****
option1
Z=CAT(Substr(X,1,5),Y)
option2
substr(X,6,5)=Y;
option3
TRANWRD(X, substr(X,6,5),Y);

Q)Name few SAS functions?
trim, Catx
CATX: concatenate character strings, removes leading and trailing blanks and inserts separators.
TRIM: trims the trailing blanks from the character values.

Q)How can I determine the position of the nth word within a character string?
Use a combination of the INDEXW and SCAN functions:pos = indexw(string,scan(string,n));

Q)I need to reorder characters within a string...use SUBSTR?
You can do this using only one function call with TRANSLATE versus two functions calls with SUBSTR. The following lines each move the first character of a 4-character string to the last:
reorder = translate('2341',string,'1234'); reorder = substr(string,2,3) substr(string,1,1);
  

Q)What is the significance of the ‘OF’ in X=SUM (OF a1-a4, a6, a9);?
Q)What is the difference between: X=a+b+c+d; and X=SUM (a, b, c, d);?
Q)What would be the result if all the arguments in SUM function are missing?

Computing Sums within an Observation

One way to compute the sum of several variables is to write a statement such as this:

SumCost = Cost1 + Cost2 + Cost3;

What if one of the Cost values is missing? This causes the sum to be missing. If you want

to ignore missing values, you can either write some DATA step logic, or simply use the

SUM function, like this:

SumCost = sum(of Cost1–Cost3);

If one or two of the Cost values are missing, SumCost is the sum of the non-missing

values. If all three Cost values are missing, the SUM function returns a missing value. If

you want the sum to be 0 if all the arguments are missing, a nice trick is to include a 0 in

the list of arguments, like this:

SumCost = sum(0, of Cost1–Cost3


 

Q)What do the MOD and INT function do? 
MOD: Modulo is a constant or numeric variable, the function returns the reminder after numeric value divided by modulo.
if a datset has 9 observations then below code returns 1st,4th,7th observation in output dataset.
 data third;
 Set old;
 If  mod(_N_,3)=1;
 Run;
INT: It returns the integer portion of a numeric value truncating the decimal portion.

Q)How might you use MOD and INT on numeric to mimic SUBSTR on character Strings?

A) The first argument to the MOD function is a numeric, the second is a non-zero numeric; the result is the remainder when the integer quotient of argument-1 is divided by argument-2. The INT function takes only one argument and returns the integer portion of an argument, truncating the decimal portion. Note that the argument can be an expression.

DATA NEW ;
A = 123456 ;
X = INT( A/1000 ) ;
Y = MOD( A, 1000 ) ;
Y1=substr(put(A,$6.),1,3);
Z = MOD( INT( A/100 ), 100 ) ;
Z1=substr(put(A,$6.),3,2);
PUT  A= X= Y= Z= ;
PUT  Y1= Z1= ;
RUN ;

Result:
A=123456
X=123
Y=456
Z=34
Y1=456
Z1=34

In ARRAY processing, what does the DIM function do?
DIM: It is used to return the number of elements in the array. When we use Dim function we would have to re –specify the stop value of an iterative DO statement if u change the dimension of the array.

What do the PAD function do?
PAD: it pads each record with blanks so that all data lines have the same length. It is used in the INFILE statement. It is useful only when missing data occurs at the end of the record.



1 comment: