Help needed with outputting data using Data Step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Help needed with outputting data using Data Step

[ Edited ]

Here I have a bunch of Checks to output if they equal 1. Each check that is equal to 0 should be ignored.

 

This is the current code I have for the output:

Data ouput_file;
    Set input_file;
    If first_check = 1;
       If second_check = 1;
          If third_check = 1;
             If fourth_check = 1;
                 If fifth_check = 1 then output;
Run;

 

At the moment, the code only works assuming that first_check = 1. The problem I am facing is that if first_check = 0, the code won't run. If first_check = 0, I still want it to ouput the other checks if they are equal to 1.

 

How do I get it to run for all checks when they are equal to 1 assuming any check (i.e the first or second) can be 0?

 

Thanks


Accepted Solutions
Solution
‎07-31-2017 12:59 PM
Super User
Posts: 19,867

Re: Help needed with outputting data using Data Step

You're conditions are stacked, like they have an AND. It looks like you want OR instead, if any is one then outout?

If so, you can simplify this by checking if the sum of the values are greater than or equal to 1. 

 

if sum(first_check, second_check, .. , fifth_check) >= 1 then output;

View solution in original post


All Replies
Solution
‎07-31-2017 12:59 PM
Super User
Posts: 19,867

Re: Help needed with outputting data using Data Step

You're conditions are stacked, like they have an AND. It looks like you want OR instead, if any is one then outout?

If so, you can simplify this by checking if the sum of the values are greater than or equal to 1. 

 

if sum(first_check, second_check, .. , fifth_check) >= 1 then output;
Respected Advisor
Posts: 4,173

Re: Help needed with outputting data using Data Step

@jos283

The syntax (sub-setting IFs) you've used translate logically into AND. What you're after is OR.

If you're variables numeric and flags (0/1) then the logic @Reeza posted will work; else use something as below:

data ouput_file;
  set input_file;
  if 
    first_check = 1 
    or second_check = 1 
    or third_check = 1 
    or fourth_check = 1 
    or fifth_check = 1 
    then output;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 130 views
  • 0 likes
  • 3 in conversation