BookmarkSubscribeRSS Feed
anilgvdbm
Quartz | Level 8

Hi Experts,

 

I need a solution in if and then conditions for multiple variables. 

 

I have data for 10 years and each year as one variable please find the example below

Cutomer ID year1 year2 year3 year4 year5 year6 year7 year8 year9 year10 Tag
1 49 12 12 37 32 26 13 14 35 21  
2 30 46 43 24 31 17 45 40 16 16  
3 25 39 32 29 30 48 32 43 35 17  
4 14 40 47 35 12 18 32 39 44 42  
5 23 12 19 42 26 24 25 30 15 41  

 

in the above table, i want to create tag as good, average and bad. how to use if and then for 10 variables at one go.

 

the conditions are if year1-year10 le 10 then Tag=good

else if year1-year10 le 10 then Tag=average;

else if  year1-year10 le 10 then Tag=poor;

 

Request you all to suggest me how to do in otherways.

 

 

Regards.

Anil

 

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I am afraid your logic does not make much sense:

"the conditions are if year1-year10 le 10 then Tag=good

else if year1-year10 le 10 then Tag=average;

else if  year1-year10 le 10 then Tag=poor;"

This all means that if year1-10 is le 10 then tag should be good, average, and poor, as all are true if any value is less or equal to then 10.

 

I mean I can do the first if for you, but I don't see how the second two logical work:

Array method:

data want;
  set have;
  length flag $20;
  array year{10};
  do i=1 to 10;
    if year{i} <= 10 then flag="good";
  end;
run;

I would like to say use a function (corrected now, this is what happens when you overthink it!):

data want;
set have;
length flag $20;
if min(of year:)<=10 then flag="good";
run;

 

novinosrin
Tourmaline | Level 20

the conditions are if year1-year10 le 10 then Tag=good

else if year1-year10 le 10 then Tag=average;

else if  year1-year10 le 10 then Tag=poor;

 

I can't understand the above

ballardw
Super User

@anilgvdbm wrote:

Hi Experts,

 

I need a solution in if and then conditions for multiple variables. 

 

I have data for 10 years and each year as one variable please find the example below

Cutomer ID year1 year2 year3 year4 year5 year6 year7 year8 year9 year10 Tag
1 49 12 12 37 32 26 13 14 35 21  
2 30 46 43 24 31 17 45 40 16 16  
3 25 39 32 29 30 48 32 43 35 17  
4 14 40 47 35 12 18 32 39 44 42  
5 23 12 19 42 26 24 25 30 15 41  

 

in the above table, i want to create tag as good, average and bad. how to use if and then for 10 variables at one go.

 

the conditions are if year1-year10 le 10 then Tag=good

else if year1-year10 le 10 then Tag=average;

else if  year1-year10 le 10 then Tag=poor;

 

Request you all to suggest me how to do in otherways.

 

 

Regards.

Anil

 


Your example data should include at least on case each of "good", "average" and "poor". And you should indicate to us which observation or row of data is which.

You rules in the if then are exactly the same for all three cases so makes little sense.

By year1-year10 do you mean "subtract year10 from year1", "all values of the variables year1 through year10", "any value of the variables year1 through year10" or something else. It is not very clear from your code or narrative which is intended.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 944 views
  • 0 likes
  • 4 in conversation