You are on page 1of 8

SAS INTERVIEWS CONCEPTUAL QUESTIONS XVI

We have a SAS Data Set:


One
----------------------------------------Date_Info
----------------------------------------24Nov is Sunday
25Nov is Monday
26Nov is Tuesday
27Nov is Wednesday
28NOV IS THURSDAY
29nov is Friday
30NOV is SATURDAY
We need to create A SAS Data SET where only days should be in uppercase and rest should be in
lowcase.
One
------------------------------------------Date_Info
------------------------------------------24nov is SUNDAY
25nov is MONDAY
26nov is TUESDAY
27nov is WEDNESDAY
28nov is THURSDAY
29nov is FRIDAY
30nov is SATURDAY
SWAP THE VALUES OF VARIABLES A AND B:
WE HAVE SAS DATA SET ONE;
DATA ONE;
X=5;
Y=3;
RUN;
CREATE ANOTHER SAS DATA SET TWO BY READING SAS DATA SET ONE:
TWO
---------X Y
3
5
WE HAVE TWO SAS DATA SETS:

Data one;
drop i j;
array a[60];
array b[60];
do j=1 to 1000;
do i=1 to 60;
a[i]=int(ranuni(i+j)*10);
b[i]=int(ranuni(j)*10);
end;
output;
end;
run;
Data two;

drop i j;
array a[60];
array b[60];
do j=1 to 1000;
do i=1 to 60;
a[i]=int(ranuni(i+j)*10);
b[i]=int(ranuni(j)*10);
end;
output;
end;
run;
WE HAVE TO CREATE A SAS DATA SET "THREE" THAT SHOULD HAVE DIFFERENCE
OF THE COMMON VARIABLES OF DATA SETS "ONE" AND "TWO".
WE HAVE A SAS DATA SET:

ONE
EMP_ID
NAME
SALARY
----------------------------------------------------101
RAM
500
102
SITA
600
103
RADHA
700
104
RAVAN
800
105
ARJUN
900
106
BHIM
1000
WE NEED TO CREATE ANOTHER SAS DATA SET:
EMP_ID
NAME
SALARY
SALARY/TOTAL
----------------------------------------------------------------------------------------------101
RAM
500
500/4700
102
SITA
600
600/4700
103
RADHA
700
700/4700
104
RAVAN
800
800/4700
105
ARJUN
900
900/4700
106
BHIM
1000
1000/4700
We have a SAS data set TEST
data test;
input Cust_ID $ visit $ visdt date7.;
format visdt date7.;
cards;
001 uns 01jun13
001 visit1 02jun13
001 visit2 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit3 06jun13
001 uns 07jun13
001 uns 08jun13
001 uns 01jun13
001 visit4 02jun13
001 visit5 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit6 06jun13
001 uns 07jun13
001 uns 08jun13
;

run;
We want the output like:
Cust_ID Vist
001
001
001
001
001
001
001
001
001
001
001
001
001
001
001
001

uns
visit1
visit2
uns
uns
visit3
uns
uns
uns
visit4
visit5
uns
uns
visit6
uns
uns

visdt
01JUN13
02JUN13
03JUN13
04JUN13
05JUN13
06JUN13
07JUN13
08JUN13
01JUN13
02JUN13
03JUN13
04JUN13
05JUN13
06JUN13
07JUN13
08JUN13

Visitseq
0.1
1.0
2.0
2.1
2.2
3.0
3.1
3.2
3.3
4.0
5.0
5.1
5.2
6.0
6.1
6.2

We have a SAS data set:


One
----X
5,6,1,10
2,3
4,7,8
9
We have to create SAS data set Two:
Two
------X
------5,6,1,10
2,3
4,7,8
9

a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
--------------------------------1 0 0 0 5 6 0 0 0 10
0 2 3 0 0 0 0 0 0 0
0 0 0 4 0 0 7 8 0 0
0 0 0 0 0 0 0 0 9 0

Variables: X A1-A10
I will always appreciate most efficient SAS program which is taking the least resources.
We have a abc.doc file having values:
ram,21,500
sita,22,600
radha,23,700
How to read abc.doc file to make a SAS data set work.abc:
work.abc
--------------Name

Age

Ram
21
Sita
22
Radha 23

Salary
500
600
700

We have a SAS data set one in which we have a variable Name:


ONE
Name
Ram Kumar m
Sita Singh
Radha Kumari Pathak
Arjun Rana

Write a SAS program to create another SAS data set TWO by reading SAS data set ONE:
TWO
Fname

Mname

Lname

Ram
Sita
Radha
Arjun

Kumar

m
Singh
Pathak
Rana

Kumari

We are reading a raw file:


111 .
.
.
.
.
.
.
. 222

We need to create a SAS dataset one like this:


ONE
Customer_Id
111

Product_ID
222

Write most efficient program.(It should be in single datastep)

We have a SAS data set:


data one;
input visit $ pt;
datalines;
v1 101
v1 102
v2 101
v2 102
v3 103
v3 101
;
run;

Required Output:
Obs

visit

_101

_102

_103

1
2
3

v1
v2
v3

1
1
1

1
1
.

.
.
1

Program:
data two;
set one;
drop i;
array a[3] _101 _102 _103;
do i=1 to dim(a);
a[i]=1;
end;
run;
proc transpose data=two out=three (drop=_name_);
by visit;
id pt;
run;
data four;
set three;
by visit;
if first.visit;
run;
proc print;
run;

Can anyone help me in write more efficient program than this as we are solving it by using three
different steps?

We are creating a user define format $country:


proc format;
value $country AU='australia'
US='United States'
Ind='India'
Sl='Sri lanka';
run;

A SAS Data set:


data one;
infile datalines;
input location $ population;
format location $country.;
datalines;
AU 1500
US 800
Ind 1000
pak 900
Sl 500
NP 300
;
run;
proc print;
run;

This is the output:

Obs
1
2
3
4
5
6

location
australia
United States
India
pak
Sri lanka
NP

population
1500
800
1000
900
500
300

In the above scenario we can see that for the observation pak and NP .$country format is not
applicable.
Write a program to count the number of observations on which $country (user defined format) is
not applied.

We have a raw dataset:


Ball Price is : $700
Bat Price is: $800
Wicket Price is : $900
We have to make SAS data set as:
Product
-------Bat
Ball
Wicket

Cost
-------700
800
900

Kindly write your answer in comment box:


We have a SAS data Set One: Write a program to create SAS data set Two.
ONE
Var1

TWO
Var2

a
b
c
d
e
f
g
h

Var1

3
5
10
10
7
7
6
9

a
b
g
e
f
h
c
d

We have a SAS data set:


SAS Data Set One
EID
101

Week
1

Attendance
5

Var2
3
5
6
7
7
9
10
10

Var3
0
0
0
1
1
0
1
1

101
101
101
102
102
103

2
3
4
1
2
3

4
6
3
1
3
6

We need to create A SAS data:


EID week1 week2 week3 week4
101
5
4
6
3
102
1
3
.
.
103
.
.
6
.

Present days
18
4
6

Write a program to create the desired data set.

You have a SAS data set one. In this SAS data set you have a variable number. You have to create
other SAS data set(TWO) and that data set should have one variable having name 'Prime_numbers', it
should contain only prime numbers from variable 'Numbers'.
--------| One |
---------

Two

Numbers
---------------17
21
55
61
88
100
15
31
50
99
89
51

Prime_Numbers
17
61
31
89
51

We have a raw data set:

Formatted Input
Ram 21 500 Friday,January 1,1960
Sita
22 600 Saturday,january 1,1960
Radha 23 700 Sunday,january 1,1960
We have to create a SAS dataset from this raw data set.
output:
Name
Ram
Sita
Radha

Age
21
22
23

Salary
500
600
700

Hire_date
01jan1960
02jan1960
03jan1960

Please write SAS Program to complete this task.

Solution:

data one;
infile 'Filename';
length x $ 10 y $ 10 z $ 10 b $ 15;
input name $ age salary @15 H_D $30.;
keep name age salary Hire_date;
x=scan(H_D,2);
z=scan(H_D,4);
a=strip(x)||strip(z);
b=tranwrd(a,substr(a,1,length(a)-4),substr(a,1,3));
day+1;
k=month(input(b,monyy7.));
Hire_Date=mdy(k,day,z);
format final date9.;
run;
proc print data=one;
run;

You might also like