0
votes

This is a SAS question. The following lines for two people are ordered by ascending AdmitNum. Ascending AdmitNum is based on ascending dates, which are omitted. Ages are provided for each AdmitNum. Age decreases between some of the observations. I don't want this to occur. Age must be equal or increase.

If the next age is less than the current age, then I want the current age to be written into the new variable NeedAge. In other words, retain the greater age while it is the greater age.

Person 2 has the wrong age, 43, in three rows. These should be 53. Person 2's age changes to 54 when AdmitNum=5 and this value, 54, should be retained.

After several attempts I have had only had partial success. Can someone suggest a way to make NeedAge as shown below? Thanks.

ID    AdmitNum  HaveAge  NeedAge               
1       1        51          51    
1       2        48          51  
1       3        51          51  
1       4        49          51  
2       1        53          53  
2       2        43          53  
2       3        43          53  
2       4        43          53  
2       5        54          54  
2

2 Answers

0
votes

Check if HaveAge exceeds NeedAge, and if so, replace NeedAge with HaveAge. Then retain.

data have;
    input ID AdmitNum HaveAge;
    datalines;          
        1 1 51
        1 2 48
        1 3 51
        1 4 49 
        2 1 53
        2 2 43
        2 3 43
        2 4 43
        2 5 54
    ;
run;

data want;
    set have;
    by ID;
    if HaveAge > NeedAge then NeedAge = HaveAge;
    retain NeedAge;
run;
0
votes
data have;
input ID AdmitNum HaveAge;
datalines;          
    1 1 51
    1 2 48
    1 3 51
    1 4 49 
    2 1 53
    2 2 43
    2 3 43
    2 4 43
    2 5 54
   ;
run;

data want;
set have;
by ID;
if _n_ = 1 NeedAge = HaveAge;
if HaveAge > NeedAge then NeedAge = HaveAge;
retain NeedAge;
run;