BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
palolix
Lapis Lazuli | Level 10

Dear SAS Community,

 

I would like to rename the variable level 'Jar' to 'Air' for the variable TRT but only at day0 and day3 (not for day7) of the variable label. The problem I am having is that SAS is renaming for day0 and day3 but ignoring day7. I want to keep the levels 'Air' and 'Jar' for day7. I know I cannot use two WHERE statements but I included the second one so you understand what I want. 

I would greatly appreciate it your help!

 

data new;
set long;
where label in('day0', 'day3');
if TRT='Jar' then TRT='Air';
where label='day7';
if TRT='Air' then TRT='Air';
if TRT='Jar' then TRT='Jar';
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You cannot have two WHERE statements in a data step.  And the placement of the WHERE statement within the DATA step makes no difference.  

 

I suspect what you want to do is include the extra conditions into your IF clauses. 

 

But I am not sure because I do not understand the terminology you are using.  You say you want to  "rename a variable level", what does that mean?   There is no variable named LEVEL referenced in your code. 

 

Perhaps you mean you want to CHANGE a VALUE of a variable?

If so then perhaps you mean you want to do this:

data new;
  set long;
  if label in ('day0' 'day3') and TRT='Jar' then TRT='Air';
run;

So for observations where LABEL is something else the value of TRT never changes.

 

 

View solution in original post

8 REPLIES 8
Tom
Super User Tom
Super User

You cannot have two WHERE statements in a data step.  And the placement of the WHERE statement within the DATA step makes no difference.  

 

I suspect what you want to do is include the extra conditions into your IF clauses. 

 

But I am not sure because I do not understand the terminology you are using.  You say you want to  "rename a variable level", what does that mean?   There is no variable named LEVEL referenced in your code. 

 

Perhaps you mean you want to CHANGE a VALUE of a variable?

If so then perhaps you mean you want to do this:

data new;
  set long;
  if label in ('day0' 'day3') and TRT='Jar' then TRT='Air';
run;

So for observations where LABEL is something else the value of TRT never changes.

 

 

palolix
Lapis Lazuli | Level 10

That is what I wanted, thank you so much Tom!

ballardw
Super User

ELSE.

You may want an if then/else construct. Otherwise ALL of those ifs get executed.

Also you very likely do not want a WHERE as that removes observations for values on the where statement are not true. So the "day7" would never exist in the data set

If this doesn't do what you want then  you should provide and example of the input data set and the desired result.

Note: the  "else if" may not be needed at all because there is no reason to change the values, but I have shown it as it seems to parallel your thinking.

 

data new;
   set long;
   IF  label in('day0', 'day3') and TRT='Jar' then TRT='Air';
   Else if  label='day7' then do;
      if TRT='Air' then TRT='Air';
      if TRT='Jar' then TRT='Jar';
  end;
run;

other code that would do the same thing:

data new;
   set long;
   IF  label in('day0', 'day3') and TRT='Jar' then TRT='Air';
run;

WHERE statement evaluates the values as they are brought into the data set. If the statement is not true then those obsesrvations, label='day7' (or any other value than day0 and day3) would not appear in the result at all. That where the limitation on single WHERE statement comes from. 

Most condition operations based on values of variables would usually be better done with IF statements. 

 

palolix
Lapis Lazuli | Level 10

Great, thank you very much!

palolix
Lapis Lazuli | Level 10

Dear SAS Community,

 

I would like to rename the variable level 'Jar' to 'Air' for the variable TRT but only at day0 and day3 (not for day7) of the variable label. The problem I am having is that SAS is renaming for day0 and day3 but ignoring day7. I want to keep the levels 'Air' and 'Jar' for day7. I know I cannot use two WHERE statements but I included the second one so you understand what I want. 

I would greatly appreciate it your help!

 

data new;
set long;
where label in('day0', 'day3');
if TRT='Jar' then TRT='Air';
where label='day7';
if TRT='Air' then TRT='Air';
if TRT='Jar' then TRT='Jar';
run;

Ksharp
Super User
data new;
set long;

if TRT='Jar' and label in ('day0', 'day3') then TRT='Air';

run;
palolix
Lapis Lazuli | Level 10

Thank you Ksharp!

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 719 views
  • 3 likes
  • 5 in conversation