BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mbayer
Calcite | Level 5

data example;
input $record_id education mother;
datalines;
1 2 1
1 . 0
1 1 0
1 . 0
2 . 0
2 3 1
3 4 1
3 . 0
;
run;
*I want it to look like this;
data example;
input $record_id education mother;
datalines;
1 2 1
1 2 0
1 2 0
1 2 0
2 3 1
2 3 0
3 4 1
3 4 0
;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
5 REPLIES 5
mbayer
Calcite | Level 5

Close! I want the education value for the observations where mother = 1 to carry over for all the other observations with the same record _id. It isn't always the max value or the first value (within that record_id). 

Kurt_Bremser
Super User
data want;
do until (last.record_id);
  set have;
  by record_id;
  if mother = 1 then _ed = education;
end;
do until (last.record_id);
  set have;
  by record_id;
  education = _ed;
  output;
end;
drop _ed;
run;
SASKiwi
PROC Star

Here's an SQL-based approach:

data example;
input record_id $ education mother;
datalines;
1 2 1
1 . 0
1 1 0
1 . 0
2 . 0
2 3 1
3 4 1
3 . 0
;
run;

proc sql;
  create table want as
  select A.record_id
        ,B.education_max
	    ,A.mother
  from example as A
  left join
  (select record_id
         ,max(education) as education_max
   from example
   where mother = 1
   group by record_id
  ) as B
  on A.record_id = B.record_id
  ;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 1362 views
  • 3 likes
  • 3 in conversation