SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
StickyRoll
Fluorite | Level 6

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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

 

View solution in original post

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

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

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 520 views
  • 0 likes
  • 2 in conversation