Help using Base SAS procedures

Consolidating responses to create a new variable

Reply
Super Contributor
Posts: 338

Consolidating responses to create a new variable

Hi Colleagues,

Would appreciate if any one of you could help me.

I have a data set like this.

Data hs;
Input ID food_run;
CARDS;
1 1
2 2
3 3
4 3
5 2
6 1
7 7
8 8
9 9
;
RUN;

In food-run variable,
1=often true
2=sometimes true
3=never true
4=don’t know
5=refusal
6=not stated

I need to create a new variable called WORRIED like this.

If the response for variable food_run = 1 or 2 then WORRIED=1
If the response for variable food_run = 3 then WORRIED=2
If the response for variable food_run = 4 or 5 or 6 then WORRIED=missing or .

I attempted like this but this does not work properly.
Data hs1;
Set hs;
If food_run = 1 or 2 then WORRIED=1;
If food_run = 2 then WORRIED=2;
Else WORRIED=.;
Run;

Could you please assist to write the correct program.

Thanks

Mirisage
Super Contributor
Posts: 359

Re: Consolidating responses to create a new variable

If food_run = 1 or 2 should be if food_run in(1,2); or
If food_run = 1 or food_run = 2;
Super Contributor
Posts: 338

Re: Consolidating responses to create a new variable

Hi Flip,

Thank you very much.

I tried your both suggestions but they didn't work. However, I removed the statement 'else .' and then both of your suggestions worked well.

Hope it is not an accident and it should be the way, isn't it?

Data hs1;
Set hs;
If food_run =1 or food_run= 2 then WORRIED=1;
If food_run = 3 then WORRIED=2;

Run;


Data hs1;
Set hs;
If food_run in(1,2)then WORRIED=1;
If food_run = 3 then WORRIED=2;

Run;


thank you

Mirisage
Super Contributor
Posts: 359

Re: Consolidating responses to create a new variable

use an else if for the second one.

else If food_run = 2 then WORRIED=2;
Super Contributor
Posts: 338

Re: Consolidating responses to create a new variable

Hi Flip, this is great!

Worked very well.

Thanks again!

Mirisage Neil
Ask a Question
Discussion stats
  • 4 replies
  • 112 views
  • 0 likes
  • 2 in conversation