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

I want to make a new variable from conditions of existing categorical variables in my dataset.

 

libname research "/home/mariamon0/";

data tobacco_;
set research.tobacco_;

 

these are the conditions:
if cigcur eq 1 or 2
if bidicur eq 1 or 2
if cigsmoker_new eq 0

 

and I want my new variable to be LCuser

 

how do I go about doing this?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You have an IF but you don't say what you want to happen when the condition is true.

There are two types of IF statements in SAS.

There is the more traditional programming structure of the IF/THEN.  

if condition then statement;

So when the condition after IF is true the statement after THEN executes.  Typically this is an assignment statement. like:

if cigsmoker_new eq 0 then LCuser=1;

The other type of IF statement in SAS takes the form:

if condition ;

This is called a subsetting IF statement.  If the condition is true then the data step iteration continues.  If it is false then the data step iteration stops.  None of the rest of the code in the data step executes, including any implicit OUTPUT at the end.  So this is equivalent to this IF/THEN statement.

if not condition then delete;

So what you want is to make a dataset that had only observations that satisfy ALL of the conditions then you could use three subsetting if statements.

data want;
  set have;
  if cigcur in (1 2);
  if bidicur in (1 2);
  if cigsmoker_new eq 0;
run;

If you want to make a new variable named LCuser and set it to one if ANY of your three conditions is true then you could use:

data want;
  set have;
  LCuser=0;
  if cigcur in (1 2) then LCuser=1;
  if bidicur in (1 2) then LCuser=1;
  if cigsmoker_new eq 0 then LCuser=1;
run;

Or you could just make one IF statement and combine the conditions.

data want;
  set have;
  LCuser=0;
  if cigcur in (1 2) or  bidicur in (1 2) or cigsmoker_new eq 0 then LCuser=1;
run;

If you combine them with AND instead of OR then LCUser will be 1 only when ALL of the conditions are true instead of when ANY of them is true.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

these are the conditions:

if cigcur eq 1 or 2
if bidicur eq 1 or 2
if cigsmoker_new eq 0


Do you mean 

 

if cigcur eq 1 or 2 AND
if bidicur eq 1 or 2 AND
if cigsmoker_new eq 0?

 

Or something else?

 

and I want my new variable to be LCuser

 

Do you want this to be YES or NO, or 0 or 1, or something else?

Can you give us the complete logic you want to follow?

--
Paige Miller
mariamon0
Fluorite | Level 6

Yes all of those conditions must be there. I want the new variable LCuser to have all of these conditions

 cigcur eq 1 or 2 AND
bidicur eq 1 or 2 AND
cigsmoker_new eq 0

Tom
Super User Tom
Super User

What does

cigcur eq 1 or 2

mean? 

SAS will interpret the OR as a binary boolean operator.  No matter what value CIGUR has it will always be TRUE since the value to right of the OR is always true.  (SAS considers and number that is not zero or missing as TRUE).

 

Did you mean:

cigcur in (1 2)
mariamon0
Fluorite | Level 6

Ok yes I wasn't aware of that. I want that variable to choose either the option 1 or the option 2. so yes I mean 

cigcur in (1 2)

bidicur in (1 2)
cigsmoker_new eq 0 

mariamon0
Fluorite | Level 6

cigcur eq 1 or 2 AND
bidicur eq 1 or 2 AND
cigsmoker_new eq 0

 

basically I want the new variable LCuser to have the following conditions

if the users cigcur said 1 or 2, if the bidicur users said 1 or 2 and if the cigsmokernew said 0.

mariamon0
Fluorite | Level 6

basically I want the new variable LCuser to have the following conditions

if the users cigcur said 1 or 2, if the bidicur users said 1 or 2 and if the cigsmokernew said 0.

 

Do I do if statements?

Or

Lcusrer=if the users cigcur said 1 or 2, if the bidicur users said 1 or 2 and if the cigsmokernew said 0;

mariamon0
Fluorite | Level 6

yes I would have to use in.

not sure what the exact coding should be together

Tom
Super User Tom
Super User

You have an IF but you don't say what you want to happen when the condition is true.

There are two types of IF statements in SAS.

There is the more traditional programming structure of the IF/THEN.  

if condition then statement;

So when the condition after IF is true the statement after THEN executes.  Typically this is an assignment statement. like:

if cigsmoker_new eq 0 then LCuser=1;

The other type of IF statement in SAS takes the form:

if condition ;

This is called a subsetting IF statement.  If the condition is true then the data step iteration continues.  If it is false then the data step iteration stops.  None of the rest of the code in the data step executes, including any implicit OUTPUT at the end.  So this is equivalent to this IF/THEN statement.

if not condition then delete;

So what you want is to make a dataset that had only observations that satisfy ALL of the conditions then you could use three subsetting if statements.

data want;
  set have;
  if cigcur in (1 2);
  if bidicur in (1 2);
  if cigsmoker_new eq 0;
run;

If you want to make a new variable named LCuser and set it to one if ANY of your three conditions is true then you could use:

data want;
  set have;
  LCuser=0;
  if cigcur in (1 2) then LCuser=1;
  if bidicur in (1 2) then LCuser=1;
  if cigsmoker_new eq 0 then LCuser=1;
run;

Or you could just make one IF statement and combine the conditions.

data want;
  set have;
  LCuser=0;
  if cigcur in (1 2) or  bidicur in (1 2) or cigsmoker_new eq 0 then LCuser=1;
run;

If you combine them with AND instead of OR then LCUser will be 1 only when ALL of the conditions are true instead of when ANY of them is true.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 1152 views
  • 1 like
  • 3 in conversation