DATA Step, Macro, Functions and more

Don't know term or code

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Don't know term or code

Data have:

 

visit_id       study_id      delivery_visit    multiple_gestation

1                      1                     0                        0

2                      1                     0                        0

3                      1                     1                        0

4                      2                     0                        1

5                      2                     1                        0

6                      3                     0                        0

7                      3                     0                        0

8                      3                     1                        0

9                      4                     0                        0

10                    4                     1                        1

 

Here is what I need to do:

For any mothers that have multiple_gestation=1 for any of the visits, then multiple_gestation=1 for all visits.

The mothers with multiple_gestation=0 would stay the same -- 0 for all visits.

 

Data want:

 

visit_id       study_id      delivery_visit    multiple_gestation

1                      1                     0                        0

2                      1                     0                        0

3                      1                     1                        0

4                      2                     0                        1

5                      2                     1                        1

6                      3                     0                        0

7                      3                     0                        0

8                      3                     1                        0

9                      4                     0                        1

10                    4                     1                        1

 

Is there a term for what I'm trying to do and what is the code?

Thanks!

 


Accepted Solutions
Solution
‎08-30-2017 10:11 AM
Super User
Posts: 5,513

Re: Don't know term or code

Coming up with a name will be difficult since there are a few ways to approach the problem.  Here's one way:

 

data want;

do until (last.study_id);

   set have;

   by study_id;

   highest = max(highest, multiple_gestation);

end;

do until (last.study_id);

   set have;

   by study_id;

   multiple_gestation = highest;

   output;

end;

drop highest;

run;

 

The top loop reads all observations for a STUDY_ID, and finds the largest value for MULTIPLE_GESTATION.

 

The bottom DO loop reads the same observations, assigns a (possibly) new value to MULTIPLE_GESTATION, and outputs the observations.

View solution in original post


All Replies
Solution
‎08-30-2017 10:11 AM
Super User
Posts: 5,513

Re: Don't know term or code

Coming up with a name will be difficult since there are a few ways to approach the problem.  Here's one way:

 

data want;

do until (last.study_id);

   set have;

   by study_id;

   highest = max(highest, multiple_gestation);

end;

do until (last.study_id);

   set have;

   by study_id;

   multiple_gestation = highest;

   output;

end;

drop highest;

run;

 

The top loop reads all observations for a STUDY_ID, and finds the largest value for MULTIPLE_GESTATION.

 

The bottom DO loop reads the same observations, assigns a (possibly) new value to MULTIPLE_GESTATION, and outputs the observations.

Regular Contributor
Posts: 234

Re: Don't know term or code

[ Edited ]

Hello,

 

Here is a solution with proc sql.

 

proc sql;
    CREATE TABLE WANT AS 
    SELECT visit_id, study_id, delivery_visit,
           max(multiple_gestation) AS multiple_gestation
    FROM have
    GROUP BY study_id
    ORDER BY visit_id;
quit;

H

Contributor
Posts: 32

Re: Don't know term or code

Both of these solutions worked perfect. Thanks to you both!

 

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 194 views
  • 2 likes
  • 3 in conversation