DATA Step, Macro, Functions and more

Modify variable values based on condition

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Modify variable values based on condition

I need to examine the profile of each patient, but it is little complicated for a novice programmer like me.

 

If it is more than one negative DAYS,then I need to eliminate all of them except that nearest the origin(in below example record of -5 will be eliminated and -1 becomes 0). If there is an observation with days = 0 then all negative days can be eliminated. If there is no observation with DAYS=0 then change the negative DAYS to DAYS=0. Please note that in the exceptional case where there is only one DAYS for a patient then it is retained even if DAYS is negative (it will of course be changed to 0)

 

 

 

data have;

   input subject days grade;

datalines;

12345  -5      0    /*deleted*/

12345   -1     0    /* -1 becomes 0*/

12345    12   1

12345    15   1

12345    21   1

12345   30    2

12345   35    2

23456    -3    0    /*deleted*/

23456    -5    0    /*deleted*/

23456     0    0   

23456     9    1

23456    13   1

34567   -5    0     /* -5 becomes 0 */
34567    8    1
34567    16   1
45678    -2   0     /* -2 becomes 0 */

run;

 I need the output to look like below,

 subject    days   grade

12345          0     0

12345         12   1

12345         15   1

12345         21   1

12345         30    2

12345         35    2

23456         0     0   

23456         9    1

23456        13   1

34567         0    0    

34567         8    1

34567       16    1

45678         0    0   

 

 

I would greatly appreciate any suggestions.

 

 


Accepted Solutions
Solution
2 weeks ago
Esteemed Advisor
Posts: 5,524

Re: Modify variable values based on condition

I think this captures it:

 

data want;
do until(last.subject);
    set have; by subject;
    if days <= 0 then maxNeg = max(maxNeg, days);   
    end;
do until(last.subject);
    set have; by subject;
    if days = maxNeg then days = 0;
    if days >= 0 then output;
    end;
drop maxNeg;
run;
PG

View solution in original post


All Replies
Solution
2 weeks ago
Esteemed Advisor
Posts: 5,524

Re: Modify variable values based on condition

I think this captures it:

 

data want;
do until(last.subject);
    set have; by subject;
    if days <= 0 then maxNeg = max(maxNeg, days);   
    end;
do until(last.subject);
    set have; by subject;
    if days = maxNeg then days = 0;
    if days >= 0 then output;
    end;
drop maxNeg;
run;
PG
Occasional Contributor
Posts: 18

Re: Modify variable values based on condition

Yes it worked. Thank you very much. I am still learning to use do until . Your code did it quickly and neatly. Thanks.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 96 views
  • 0 likes
  • 2 in conversation