Monday, June 22, 2009

SAS:Concepts And Questions

Question1:-

HOW TO OBTAIN LAST N OBS FROM A SAS DATASET IF WE DON'T KNOW THE NUMBER OF OBS..?

Sol-:
If you want to obtain last 5 obs (or n obs) then here is a simple way of doing -

data last_five;
set input_data nobs = t_obs;

if _n_ gt (t_obs - 5) then output last_five;

run;
(first answered by-anonymous,SAS orkut community)

Question2:-
WHAT IS THE AUTOMATIC VARIABLE _N_ DOES IN SAS?

A)COUNTS THE NUMBE OF OBSERVATIONS IN DATASET
B)COUNTS THE ITERATIONS OF DATASTEP

Sol-:
viks only (b) is always correct. some time no. of iterations of datastep is equal of no. of observations in dataset.

let see this code......

data madhur;
input ......; /* team is third variable in datalines*/

datalines;
1023 David red 189 165
1049 Amelia yellow 145 124
1219 Alan red 210 192
1246 Ravi yellow 194 177
1078 Ashley red 127 118
1221 Jim yellow 220 .
;

proc print data=madhur;
run;

u see in this code output dataset (madhur) will have 6 observations and datastep also iterate 6 times to write these 6 observations into output dataset madhur.

now see same code with subsetting if statement.


data madhur;
input ......; /* team is third variable in datalines*/
if Team='red';

datalines;
1023 David red 189 165
1049 Amelia yellow 145 124
1219 Alan red 210 192
1246 Ravi yellow 194 177
1078 Ashley red 127 118
1221 Jim yellow 220 .
;

proc print data=madhur;
run;

u see in this code output dataset madhur will have 3 observations ( due to subsetting if statement ) but datastep has to iterate 6 times to select 3 observations ( for which the values of team is red ).

(first answered by-manu,SAS orkut community)


Question3:-
CAN U TELL THE OUTPUT OF THE FOLLOWING CODE-

Data Data1;
Test='X';
Select(Test);
When('Y') Name='Viks';
When('X') Name='Viks@Home';
When('Z') Name='Other';
Otherwise Name='Unknown';
End;
Run;

What Is The Reason For This Output?
Plz Answer Without Running In Sas...U May Chk In Sas After U Answer.

Sol-:
without running i guessed the answer to be : 'viks@home'

but length of variable screwed up the answer. coz a statement before that (when('y)) set the length to be $4. in the same statment if u use [ length name $10. ] u will get name='viks@home'.

(first answered by-Varun Joshi,SAS orkut community)

Question4:-
write code to reverse order of observations in a dataset.
e.g. if a dataset has following observations:
ID Name color height weight
---------------------------------
1023 David red 189 165
1049 Amelia yellow 145 124
1219 Alan red 210 192
1246 Ravi yellow 194 177
1078 Ashley red 127 118
1221 Jim yellow 220 .

output should have David at last and Jim in first place. (assume data is not sorted in any key: means don't use descending sort)


Sol-:
data raw;
srt_var=_n_;
input id name$ color$ height weight;
datalines;
1023 David red 189 165
1049 Amelia yellow 145 124
1219 Alan red 210 192
1246 Ravi yellow 194 177
1078 Ashley red 127 118
1221 Jim yellow 220 .
;
run;

proc sort data=raw out=rev(drop=srt_var);by descending srt_var;run;

proc print data=rev noobs;run;

(answered by-Varun Joshi,Sas orkut community)

Send link to --

TopBlogDir.blogspot.com button Education Blog Directory Academic,  Learning & Educational Blogs - Blog Catalog Blog Directory SEO Court Directory myblog Visit blogadda.com to discover Indian blogs Add to Technorati Favorites Subscribe with Bloglines DigNow.net

Join My Community at MyBloglog!

Free Web Directory - Add Your Link
The Little Web Directory
Monster Directory A List Sites