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

Regading if and then

Reply
Contributor
Posts: 57

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??

thanks

Anil

Super User
Super User
Posts: 7,392

Re: Regading if and then

Hi,


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

data want;

     set have;

     vegetables=0;

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

...

Maybe think of it slightly differently:

data want;

     set have;

     vegeables=0;

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

run;

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

Regular Contributor
Posts: 151

Re: Regading if and then

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

data want;

set have;

vegetable = max(onion,potato,cauli_flower);

run;

Ask a Question
Discussion stats
  • 2 replies
  • 288 views
  • 3 likes
  • 3 in conversation