Hello, I have the following sample input data:
data sorteddata;
POL_ID='AAA';
POL_CLS='BLUE';
POL_YEAR='2024';
MAD_AGE = 26;
output;
POL_ID='AAA';
POL_CLS='BLUE';
POL_YEAR='2024';
MAD_AGE = 21;
output;
POL_ID='AAA';
POL_CLS='BLUE';
POL_YEAR='2024';
MAD_AGE = 56;
output;
POL_ID='BBB';
POL_CLS='GREY';
POL_YEAR='2025';
MAD_AGE = 18;
output;
run;
proc sort data=sorteddata;
by POL_ID POL_CLS POL_YEAR MAD_AGE;
run;
I have the following code that will allow me to mark the first unique set of values with Flag ='Y'.
data mad_age;
set sorteddata;
by POL_ID POL_CLS POL_YEAR MAD_AGE;
if first.POL_ID and first.POL_CLS and first.POL_YEAR and MAD_AGE then flag='Y';
run;
Once this is done, the column FLAG will have value 'Y' for every first record of POL_ID POL_CLS and POL_YEAR and MAD_AGE.
Now next step, how can i create a new field called "YOUNGEST_PLAYER" and for every first record of POL_ID POL_CLS POL_YEAR and MAD_AGE, for example, the age is 25 (so the value of FLAG = 'Y'), how do i populate 25 for the same group of other records belonging to the same POL_ID POL_CLS POL_YEAR combination?
For example:
POL_ID POL_CLS and POL_YEAR and MAD_AGE FLAG
1 A 2024 25 Y
1 A 2025 70
1 A 2025 71
into
POL_ID POL_CLS and POL_YEAR and MAD_AGE FLAG YOUNGEST_PLAYER
1 A 2024 25 Y 25
1 A 2025 70 25
1 A 2025 71 25
Since your data is already sorted the wayt you want it, you can do this
data want;
set mad_age;
by POL_ID POL_CLS POL_YEAR MAD_AGE;
if first.POL_ID then youngest_player = mad_age;
retain youngest_player;
run;
Result:
POL_ID POL_CLS POL_YEAR MAD_AGE flag youngest_player AAA BLUE 2024 21 Y 21 AAA BLUE 2024 26 21 AAA BLUE 2024 56 21 BBB GREY 2025 18 Y 18
Since your data is already sorted the wayt you want it, you can do this
data want;
set mad_age;
by POL_ID POL_CLS POL_YEAR MAD_AGE;
if first.POL_ID then youngest_player = mad_age;
retain youngest_player;
run;
Result:
POL_ID POL_CLS POL_YEAR MAD_AGE flag youngest_player AAA BLUE 2024 21 Y 21 AAA BLUE 2024 26 21 AAA BLUE 2024 56 21 BBB GREY 2025 18 Y 18
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.