BookmarkSubscribeRSS Feed
anilgvdbm
Quartz | Level 8

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

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Keith
Obsidian | Level 7

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

data want;

set have;

vegetable = max(onion,potato,cauli_flower);

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 821 views
  • 3 likes
  • 3 in conversation