BookmarkSubscribeRSS Feed
Eugenio211
Quartz | Level 8

hello,

 

hope all is good.  I want to use an if else then do statement to generate 1, 0 and blank on a data step with the below data,  I am getting a road block as my statements are not correct.  can someone assist? thanks a lot.

 

EMAIL_ADDR HomePh CellPhn CONSENT_END_DATE
myemail@gmail.com 1234567890 9876543210  
myemail@gmail.com 1234567890 9876543210  
myemail@gmail.com 1234567890 9876543210 12Aug2017 12:19:22.237
myemail@gmail.com 1234567890 9876543210 12Aug2017 12:19:22.237
myemail@gmail.com 1234567890 9876543210 28Feb2018 10:29:18.003
myemail@gmail.com 1234567890 9876543210 28Feb2018 10:29:18.003

 

this is the data step that I have:

 

 
data Optin; set test;
if Email_Addr ~= . then do;
if Email_Addr = . then Email_OptIn = '';
else if Email_Addr = Channel_Value and Consent_End_Date ~= . then EMail_OptIn = 1;
else if Email_Addr ~= . and Channel_value ~= . and Consent_End_Date = . then Email_OptIn = 1;
end;
run;
 
 
I want to do this for email_addr, HomePhone and Cell Phone.
2 REPLIES 2
Reeza
Super User
data Optin; 

set test;

if not missing(Email_Addr) then do;

    if missing(Email_Addr) then call missing(email_optin);
    else if Email_Addr = Channel_Value and missing(Consent_End_Date)  then EMail_OptIn = 1;
    else if not missing(Email_addr) and not missing(Channel_value) and missing(Consent_End_Date) then Email_OptIn = 1;

end;

run;

I've converted your code to use missing and call missing() as they're easier to read/follow IMO.

Let's look at what the program.

 

 If not missing email address, then do this set of instructions:

1. If missing email address set opt in to missing -> because this is nested in the if not missing portion, this will never be true.

2. Check if the email address is the same as the channel value, and you're not missing a consent end date, then set opt in = 1. Channel value is not shown so no way to check this logic.

3.  If not missing channel value, and not missing email address (already checked for before you entered this step) and missing consent date, set optin to 1. 

 

The logic here seems flawed but I have no idea what it should be....

 

 

+


@Eugenio211 wrote:

hello,

 

hope all is good.  I want to use an if else then do statement to generate 1, 0 and blank on a data step with the below data,  I am getting a road block as my statements are not correct.  can someone assist? thanks a lot.

 

EMAIL_ADDR HomePh CellPhn CONSENT_END_DATE
myemail@gmail.com 1234567890 9876543210  
myemail@gmail.com 1234567890 9876543210  
myemail@gmail.com 1234567890 9876543210 12Aug2017 12:19:22.237
myemail@gmail.com 1234567890 9876543210 12Aug2017 12:19:22.237
myemail@gmail.com 1234567890 9876543210 28Feb2018 10:29:18.003
myemail@gmail.com 1234567890 9876543210 28Feb2018 10:29:18.003

 

this is the data step that I have:

 

 
data Optin; set test;
if Email_Addr ~= . then do;
if Email_Addr = . then Email_OptIn = '';
else if Email_Addr = Channel_Value and Consent_End_Date ~= . then EMail_OptIn = 1;
else if Email_Addr ~= . and Channel_value ~= . and Consent_End_Date = . then Email_OptIn = 1;
end;
run;
 
 
I want to do this for email_addr, HomePhone and Cell Phone.

 

Tom
Super User Tom
Super User

You should not attempt to compare a CHARACTER variable like EMAIL_ADDR to a NUMERIC constant like .

If you want to tell if a character variable is missing you can compare it to an empty string like ' '.

If you want to test if a variable is missing and aren't sure if it will be a numeric or a character variable then just use the MISSING() function.

 

Do you want Email_OptIn to be a character variable? Like in your first reference to it?

Email_OptIn = '';

Or do you want it to be a numeric variable? Like in your later references to it.

EMail_OptIn = 1;

I am not sure what logic you are trying to implement.  I am also not sure what you think the ~ symbol means.  Are you trying to use it as a substitute for the NOT operator?

 

Can you describe in words what situations lead to EMail_OptIn = 1? 

And what situations should lead to EMail_OptIn = 0

and what situations should lead to EMail_OptIn = , 

 

Can you show the expected result for your example input?

 

 

 

 

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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