- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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";
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for your reply.
I have just discovered that my data is truncated which solves my problem with understanding the code.