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-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!

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