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

Data set is below.

 

I would like to retain the Max YEAR with the corresponding FIRST_FOUND detail. I've tried a retain statement but it seems its not working. Not sure if I am able to create a MAX(YEAR) within in a data step or if i will have to create that in a proc sql prior to performing my data step. Any assistance would be appreciated. 

 

Primary_IDSecondary_IDYEARCONDITIONSTATUSFIRST_FOUNDcount
1ABCDEFG234545672014108NEWIT0
1ABCDEFG234545672015108DROPPED 1
1ABCDEFG234545672016108DROPPED 2
1ABCDEFG234545672017108DROPPED 3
1ABCDEFG234545672018108DROPPED 4
1ABCDEFG234545672019108DROPPED 5

 

This is how i would like my data to look 

Primary_IDSecondary_IDDOS_YEARCONDITION STATUSFIRST_FOUNDcount
1ABCDEFG234545672018108NEWIT5

 

Code Below:

 

data source_detail_1 (keep=Primary_ID Secondary_ID YEAR CONDITION STATUS FIRST_FOUND COUNT YEAR1 );
set source_detail (where=(PRIMARY_ID='1ABCDEFG2345')) ;
retain first_found;
YEAR1= MAX(YEAR);
run;

1 ACCEPTED SOLUTION

Accepted Solutions
bknitch
Quartz | Level 8

Solved myself 

 

proc sort data=WORK.SOURCE_DETAIL; by primary_id year descending first_found ;run;
data temp1;
set WORK.SOURCE_DETAIL;
length first_found1 $3.;
retain first_found1 ;
by primary_id year descending first_found ;
if first.primary_id then do;
first_found1=first_found;
end;
output;
drop first_found;

run;

View solution in original post

1 REPLY 1
bknitch
Quartz | Level 8

Solved myself 

 

proc sort data=WORK.SOURCE_DETAIL; by primary_id year descending first_found ;run;
data temp1;
set WORK.SOURCE_DETAIL;
length first_found1 $3.;
retain first_found1 ;
by primary_id year descending first_found ;
if first.primary_id then do;
first_found1=first_found;
end;
output;
drop first_found;

run;

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
  • 1 reply
  • 689 views
  • 0 likes
  • 1 in conversation