Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Regading if and then

Posts: 59

Regading if and then

Dear Forum,

i have one question for you all i am doing scoring for one nutrition data.

i have sample data like this

onoin        potato       cauli_filower     vegetables

1                 0                 1

0                  1                1

1                  1                1

0                  0                1

0                 0                  0

so i am going to creat new variable called vegetables suppose the respondent consumes onion and cauli flower the i need to enter 1

i need result like

1   1     1   =1

0   1     0   =1

0    0     0  =0

1    1     0   =1

so for this i need to use if and then condition in between the if and then i need to use 'and' also please give me some i dea wether it works or not??



Super User
Super User
Posts: 7,942

Re: Regading if and then

Posted in reply to anilgvdbm


You could use nested if statements, however it could get quite messy e.g:

data want;

     set have;


     if onion=1 or potato=1  or calui_flower=1 then vegetables=1;


Maybe think of it slightly differently:

data want;

     set have;


     if sum(onion,potato,cauli_flower)>0 then vegetables=1;


There are other functions also that could do a similar thing.

Regular Contributor
Posts: 151

Re: Regading if and then

Posted in reply to anilgvdbm

You don't even need to use an if, then statement.

data want;

set have;

vegetable = max(onion,potato,cauli_flower);


Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation