SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Marleneek
Calcite | Level 5

Hi!

 

I have a character variable, class, that can equal the following values; 1A, 1B, 1C and 1D.

 

I would normally write out what the character variable ought to be equal to, like the following code:

data test1; set test;

  if class="1A" and class="1B" then newclass="1A"

drop class; run;

 

I have sometimes stumbled upon the code below. My question is if the code below equals or will produce the same results as the code above? 

data test1; set test;

  if class="1A&1B" then newclass="1A"

drop class; run;

 

I have also stumbled upon this particular code below. Is this the same as the one above?:

data test1; set test;

  if class="1A, 1B" then newclass="1A"

drop class; run;

 

Best regards!

 

 

6 REPLIES 6
Astounding
PROC Star

A few notes ...

 

None of these are the same.

 

It is not possible for CLASS to be both "1A" and "1B" on the same observation.  Most likely you meant to use OR instead of AND:

 

if class = "1A" or class = "1B" then newclass="1A";

 

For that statement, there is an equivalent:

 

if class in ("1A" "1B") then newclass="1A";

Kurt_Bremser
Super User
data test1; set test;
  if class="1A" and class="1B" then newclass="1A"
drop class; run;

Can't work. The condition can never be true, as the two parts of the "and" are mutually exclusive. Could only work if used with an "or".

data test1; set test;
  if class="1A&1B" then newclass="1A"
drop class; run;

Tests for the literal string "1A&1B". Since 1B can't be a macro variable name, no macro resolution will be done (& is a macro trigger).

data test1; set test;
  if class="1A, 1B" then newclass="1A"
drop class; run;

Once again, tests for the literal string "1A, 1B" (including the blank).

 

What are you trying to achieve?

Marleneek
Calcite | Level 5

I have to use code from another SAS program not written by me, which is why I am trying to understand what class="1A, 1B" means and how SAS runs this command. When I e.g. run the code below, I don't get any observations in my new dataset "english_a_b", which seems strange. It is always the case when I use if class="1A, 1B" or some variation like '1A, 1B, 1C'. Maybe SAS test for the literal string and does not find a match, which is why I don't get any observations..

 

data english_a_b; set teacherenglish;
if class ne '1A, 1B" then delete; run;
Astounding
PROC Star

Exactly.  SAS is looking for a match on the entire string, not a match on pieces within the string.

 

Functions might be able to search for pieces, such as:

 

if findw('1A, 1B', class) then do;

 

But the IN operator is still safer:

 

if class in ('1A' '1B') then do;

 

Someone else wrote the program?  It could still be wrong.

Kurt_Bremser
Super User

@Marleneek wrote:

I have to use code from another SAS program not written by me, which is why I am trying to understand what class="1A, 1B" means and how SAS runs this command. When I e.g. run the code below, I don't get any observations in my new dataset "english_a_b", which seems strange. It is always the case when I use if class="1A, 1B" or some variation like '1A, 1B, 1C'. Maybe SAS test for the literal string and does not find a match, which is why I don't get any observations..

 

data english_a_b; set teacherenglish;
if class ne '1A, 1B" then delete; run;

This code is very obviously rubbish. Please post code that does more than just be a syntax error.

Marleneek
Calcite | Level 5

Thank you for your reply. 

 

I have just discovered that my data is truncated which solves my problem with understanding the code.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register 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
  • 6 replies
  • 1729 views
  • 0 likes
  • 3 in conversation