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 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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