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

Hello everyone,

 

I need to use WEIGHTED_PD columns based on YEAR_DIFF column. For example, if YEAR_DIFF=3 then NEW_COLUMN must get the value in column of WEIGHTED_PD3. However, it must get WEIGHTED_PD1 if YEAR_DIFF=0. Thanks in advance.

 

Kymet_0-1735586868402.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Kıymet wrote:

Hello everyone,

 

I need to use WEIGHTED_PD columns based on YEAR_DIFF column. For example, if YEAR_DIFF=3 then NEW_COLUMN must get the value in column of WEIGHTED_PD3. However, it must get WEIGHTED_PD1 if YEAR_DIFF=0. Thanks in advance.

 

Kymet_0-1735586868402.png


Are you asking how to use YEAR_DIFF to index into an ARRAY?  Assuming you mean to use index of 1 when year_diff is less than 1 then perhaps just use a MAX() function call?  If the rule is more complex you might need to instead use an IF/THEN construct.

 

So if you have 5 WEIGHTED_PDx variables in your dataset then you program might look like this:

data want;
  set have;
  array pd WEIGHTED_PD1-WEIGHTED_PD5;
  new_column=pd[max(1,year_diff)];
run;

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

There is no WEIGHTED_PD1 column.

Please help us and provide data as working SAS data step code (examples and instructions) and not as a screen capture.

--
Paige Miller
Tom
Super User Tom
Super User

@Kıymet wrote:

Hello everyone,

 

I need to use WEIGHTED_PD columns based on YEAR_DIFF column. For example, if YEAR_DIFF=3 then NEW_COLUMN must get the value in column of WEIGHTED_PD3. However, it must get WEIGHTED_PD1 if YEAR_DIFF=0. Thanks in advance.

 

Kymet_0-1735586868402.png


Are you asking how to use YEAR_DIFF to index into an ARRAY?  Assuming you mean to use index of 1 when year_diff is less than 1 then perhaps just use a MAX() function call?  If the rule is more complex you might need to instead use an IF/THEN construct.

 

So if you have 5 WEIGHTED_PDx variables in your dataset then you program might look like this:

data want;
  set have;
  array pd WEIGHTED_PD1-WEIGHTED_PD5;
  new_column=pd[max(1,year_diff)];
run;

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 2 replies
  • 527 views
  • 2 likes
  • 3 in conversation