DATA Step, Macro, Functions and more

IF statement

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

IF statement

What does it mean when an if statement is only followed by the name of a library?


Accepted Solutions
Solution
‎01-20-2016 11:39 AM
Super User
Posts: 5,516

Re: IF statement

As a new user, it's understandable that you wouldn't understand:

 

IF FIRST.SHORT_ACCOUNT_NUMBER;

 

It's actually more complicated than it looks, so I can give you the overview only.

 

IF deletes observations, and continues to process other observations.  The ones that satisfy the IF condition continue to be processed, and the others get deleted.

 

FIRST.SHORT_ACCOUNT_NUMBER is created in a DATA step using a BY statement.  What it does, and how it works, will take some study on your part.  (You have already been pointed to some reference material.)

 

The net result in this program handles cases where there are multiple observations for the same SHORT_ACCOUNT_NUMBER:

 

  • Just keep the first observation for each SHORT_ACCOUNT_NUMBER
  • Delete any remaining observations for the same SHORT_ACCOUNT_NUMBER

Hope this at least points you in the right direction.

View solution in original post


All Replies
Super User
Posts: 5,435

Re: IF statement

You mean "name of a variable"?

 

If that's the case, you are referring to a "subsetting if". It means that any observation that does meet the criteria, is not processed further on in the data step. So any explicit or implicit output will not be performed.

 

If you just name a variable, for a numerical value of 0 means false (subset will occur), any other value is evaluated as true (processing will proceed with the following steps).

 

Do you have an example?

Try to execute it, evaluate the results. Potentially change input data/IF statement and see the differences.

Data never sleeps
New Contributor
Posts: 2

Re: IF statement

for example:

DATA CR_OPEN_IO_PART_TEMP_1;

SET CR_OPEN_IO_PART_TEMP;

BY SHORT_ACCOUNT_NUMBER;

IF FIRST.SHORT_ACCOUNT_NUMBER;

RUN;

 

 

Respected Advisor
Posts: 3,799
Super User
Super User
Posts: 7,979

Re: IF statement

The by statement:

BY SHORT_ACCOUNT_NUMBER;

Creates a binary value, 1 for true, 0 for false, which is set based on whether a value is the first occurrence of a value in that variable.  A bit like:

SHORT_ACCOUNT_NUMBER      ISFIRST

A                                               1

A                                               0

...

 

The if:

IF FIRST.SHORT_ACCOUNT_NUMBER;

Is the same as saying: if isfirst=1 then output;

 

Please dont code all in uppercase, it makes code painful to read.

Solution
‎01-20-2016 11:39 AM
Super User
Posts: 5,516

Re: IF statement

As a new user, it's understandable that you wouldn't understand:

 

IF FIRST.SHORT_ACCOUNT_NUMBER;

 

It's actually more complicated than it looks, so I can give you the overview only.

 

IF deletes observations, and continues to process other observations.  The ones that satisfy the IF condition continue to be processed, and the others get deleted.

 

FIRST.SHORT_ACCOUNT_NUMBER is created in a DATA step using a BY statement.  What it does, and how it works, will take some study on your part.  (You have already been pointed to some reference material.)

 

The net result in this program handles cases where there are multiple observations for the same SHORT_ACCOUNT_NUMBER:

 

  • Just keep the first observation for each SHORT_ACCOUNT_NUMBER
  • Delete any remaining observations for the same SHORT_ACCOUNT_NUMBER

Hope this at least points you in the right direction.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 244 views
  • 2 likes
  • 5 in conversation