BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Dataset:

 

subj   q1     q2     q3  newvar

1        1       3      3

2        2       .      1

3        2       1      3

4        3       2      2

5        1       5     1

6        2       4     3

7        3       3     5

 

How would I create newvar, where if q1 is answered as "1", then the negative of q2 is the value in newvar? And if q1 is answered as "2", then the newvar value is 0? And if "3" is answered, then q3 is the value in newvar?

 

so newvar would look like:  subj  newvar

                                             1       -3

                                             2        0

                                             3        0

                                             4        2

                                             5       -5

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

This is a basic IF statement. 

 

data data_plus_new_var;
set have;

if q1=1 then new_var=-1*q2;
else if q1=2 then new_var=0;
else if q1=3 then new_var=q3;

run;

proc print data=data_plus_new_var;
run;

Here's an intro into creating new variables in SAS:

http://www.ats.ucla.edu/stat/sas/modules/vars.htm

View solution in original post

2 REPLIES 2
Reeza
Super User

This is a basic IF statement. 

 

data data_plus_new_var;
set have;

if q1=1 then new_var=-1*q2;
else if q1=2 then new_var=0;
else if q1=3 then new_var=q3;

run;

proc print data=data_plus_new_var;
run;

Here's an intro into creating new variables in SAS:

http://www.ats.ucla.edu/stat/sas/modules/vars.htm

ballardw
Super User

Basic use of IF /Then statements.

 

data want;
   set have;
   if q1=1 then newvar= (-1) * q2;
   else if q1=2 then newvar=0;
   else if q1=3 then newvar=q3;
run;

is one way.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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