Base SAS Interview Questions and Answers Part – 4

31. How to label values and use it in PROC FREQ?

Ans: Use PROC FORMAT to set up a format.

proc format;
value score 0 – 100 = ‘100-‘
101 – 200 = ‘101+’
other = ‘others’
;

proc freq data= readin;
tables outdata;
format outdata score. ;
run;

32. How to use arrays to recode set of variables?

Ans: Recode the set of questions: Q1,Q2,Q3…Q20 in the same way: if the variable has a value of 6 recode it to SAS missing.

data readin;
set outdata;
array Q(20) Q1-Q20;
do i= 1 to 20;
if Q(i) = 6 then Q(i)= .;
end;
run;

33. How to use arrays to recode all the numeric variables?

Ans: Use _numeric_ and dim functions in array.

data readin;
set outdata;
array Q(*) _numeric_;
do i= 1 to dim(Q);
if Q(i) = 6 then Q(i)= .;
end;
run;

Note : DIM returns a total count of the number of elements in array dimension Q.

34. How to calculate mean for a variable by group?

Ans: Suppose Q1 is a numeric variable and Age a grouping variable. You wish to compute mean for Q1 by Age.

PROC MEANS DATA = READIN;
VAR Q1;
CLASS AGE;
RUN;

35. How to generate cross tabulation?

Ans: Use PROC FREQ code.

PROC FREQ DATA=auto;
TABLES A*B ;
RUN;

SAS will produce table of A by B.

36. How to generate detailed summary statistics?

Ans: Use PROC UNIVARIATE code.

PROC UNIVARIATE DATA= READIN;
CLASS Age;
VAR Q1;
RUN;
Note : Q1 is a numeric variable and Age a grouping variable.

37. How to count missing values for numeric variables?

Ans: Use PROC MEANS with NMISS option.

38. How to count missing values for all variables?

Ans: proc format;
value $missfmt ‘ ‘=’Missing’ other=’Not Missing’;
value missfmt . =’Missing’ other=’Not Missing’;
run;

proc freq data=one;
format _CHAR_ $missfmt.;
tables _CHAR_ / missing missprint nocum nopercent;
format _NUMERIC_ missfmt.;
tables _NUMERIC_ / missing missprint nocum nopercent;
run;

39. Describe the ways in which you can create macro variables

Ans: There are 5 ways to create macro variables:

  1. %Let
  2. Iterative %DO statement
  3. Call Symput
  4. Proc SQl into clause
  5. Macro Parameters.

40. Use of CALL SYMPUT

Ans: CALL SYMPUT puts the value from a dataset into a macro variable.

proc means data=test;
var x;
output out=testmean mean=xbar;
run;

data _null_;
set testmean;
call symput(“xbarmac”,xbar);
run;

%put mean of x is &xbarmac;