Subsetting SAS dataset is using some conditions and selecting observation from one dataset and creating the other. This may be useful when one want to work a particular set of observation rather than working the whole data. Moreover, this will help us to keep the original file undisturbed. In this post we will see how to subset the data from one dataset to the other .
#Problem:
We want to create a dataset from the hospital data (given the Ron cody book) where the admission data falls on Monday and the year is 2002. We also want to know the age of the patient as of the admission date.
Hospital Dataset:
#Solution:
#Output:
# About the program and Learning:
In this program we subset the original Hosp dataset to a new called monday02. This can be done by using SET function. The set function helps us to subset the observation from the original dataset. By giving any particular condition next to set function helos us to get the particular data or if we need the whole data we simply use the set function alone. In this programm we also used WEEKDAY function which helps us to compute the day of the week from the SAS date. Then we used yrdif and actual which we learned already.
#Problem:
We want to create a dataset from the hospital data (given the Ron cody book) where the admission data falls on Monday and the year is 2002. We also want to know the age of the patient as of the admission date.
Hospital Dataset:
data A15028.A28_hosp; do j = 1 to 1000; AdmitDate = int(ranuni(1234)*1200 + 15500); quarter = intck('qtr','01jan2002'd,AdmitDate); do i = 1 to quarter; if ranuni(0) lt .1 and weekday(AdmitDate) eq 1 then AdmitDate = AdmitDate + 1; if ranuni(0) lt .1 and weekday(AdmitDate) eq 7 then AdmitDate = AdmitDate - int(3*ranuni(0) + 1); DOB = int(25000*Ranuni(0) + '01jan1920'd); DischrDate = AdmitDate + abs(10*rannor(0) + 1); Subject + 1; output; end; end; drop i j; format AdmitDate DOB DischrDate mmddyy10.; run;
#Solution:
data A15028.A28_monday02; *subsetting the data hosp; set A15028.A28_hosp; where year(AdmitDate) eq 2002 and weekday(AdmitDate) eq 2; Age = round(yrdif(DOB,AdmitDate,'Actual')); run; proc print data=A15028.A28_monday02; run;
#Output:
# About the program and Learning:
In this program we subset the original Hosp dataset to a new called monday02. This can be done by using SET function. The set function helps us to subset the observation from the original dataset. By giving any particular condition next to set function helos us to get the particular data or if we need the whole data we simply use the set function alone. In this programm we also used WEEKDAY function which helps us to compute the day of the week from the SAS date. Then we used yrdif and actual which we learned already.
No comments:
Post a Comment