BookmarkSubscribeRSS Feed
Mirisage
Obsidian | Level 7
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
4 REPLIES 4
Flip
Fluorite | Level 6
If food_run = 1 or 2 should be if food_run in(1,2); or
If food_run = 1 or food_run = 2;
Mirisage
Obsidian | Level 7
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
Flip
Fluorite | Level 6
use an else if for the second one.

else If food_run = 2 then WORRIED=2;
Mirisage
Obsidian | Level 7
Hi Flip, this is great!

Worked very well.

Thanks again!

Mirisage Neil

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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