BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
eabc0351
Quartz | Level 8

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

3 REPLIES 3
Astounding
PROC Star

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.

gamotte
Rhodochrosite | Level 12

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

eabc0351
Quartz | Level 8

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

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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