BookmarkSubscribeRSS Feed
jnivi
Calcite | Level 5

Hi there,

 

I have created a new character variable in my dataset called port, that is dependent on the values from a different character variables. 

For this new port variable, I want the values to equal the land character variable only if the new port variable value is missing, and the values in the land variable does not start with "Unknown", "Other" , "Rail".  I have tried the following code, but it appears to replace the port values with the land values i did not want. I am hoping someone can provide some suggestion to get this code working properly! I have provided an example of my code below 

 

data newloc;
set othport;
length port$200;

if port="" and land ^=:"Unknown" or land ^=:"Other" or land ^=:"Rail" 
then port= land;
run;

 

Thank you !

3 REPLIES 3
SASKiwi
PROC Star

Your logic needs to use AND in this case

data newloc;
set othport;
length port$200;

if port="" and land ^=:"Unknown" AND land ^=:"Other" AND land ^=:"Rail" 
then port= land;
run;

:

 

Tom
Super User Tom
Super User

Just try a few example values and do the comparisons in your head and you will see the problem with your logic.

 

If the value starts with Unknown the first test if FALSE but the other two are TRUE.  Similar result for the other prefixes you are testing for.  And if the value is none of them then all three tests are TRUE.

 

You want AND.

 

Or just use the IN operator instead.

if port=" " and land ^in: ("Unknown" "Other" "Rail") then port=land;
s_lassen
Meteorite | Level 14

I think the problem is with the precedence of operators. AND has higher precedence than OR, just like * has higher precedence than +. So your expression actually evaluates to

(port="" and land ^=:"Unknown") or land ^=:"Other" or land ^=:"Rail" 

You will have to use a parathesis to get what you want:

port="" and (land ^=:"Unknown" or land ^=:"Other" or land ^=:"Rail") 

or use the in: operator, as suggested by @Tom 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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