Solved
Contributor
Posts: 46

# The use of Output and Retain here?

``````if delflag=0 then output;
if happen=1 then delflag=1;``````

in the above two statement,

the lines with delflag=0 have been written to the want dataset and

how does the next if clause still truncate the rest of observations against the same id?

and even leave the first "happen=1" in the dataset?

I am trying to learn this logic.  your response is very appreicated.

Accepted Solutions
Solution
‎04-22-2016 03:44 PM
Posts: 1,256

## Re: The use of Output and Retain here?

Hello @Lulus,

Here is what happens with each observation of your example dataset when mohamed_zaki's data step is executed (focusing on the three lines of code starting with "if"):

```ID seq happen   comment
1   1    0      delflag is set to 0, obs. (incl. delflag) is written to WANT due to OUTPUT statement
1   2    0      delflag is still 0 due to RETAIN, obs. is written to WANT
1   3    1      delflag is still 0, obs. is written to WANT, delflag is set to 1
2   1    0      delflag is set to 0, obs. is written to WANT
2   2    1      delflag is still 0, obs. is written to WANT, delflag is set to 1
2   3    0      delflag is still 1, hence obs. is not written to WANT
2   4    1      delflag is still 1, hence obs. is not written to WANT, delflag is set to 1```

All Replies
Solution
‎04-22-2016 03:44 PM
Posts: 1,256

## Re: The use of Output and Retain here?

Hello @Lulus,

Here is what happens with each observation of your example dataset when mohamed_zaki's data step is executed (focusing on the three lines of code starting with "if"):

```ID seq happen   comment
1   1    0      delflag is set to 0, obs. (incl. delflag) is written to WANT due to OUTPUT statement
1   2    0      delflag is still 0 due to RETAIN, obs. is written to WANT
1   3    1      delflag is still 0, obs. is written to WANT, delflag is set to 1
2   1    0      delflag is set to 0, obs. is written to WANT
2   2    1      delflag is still 0, obs. is written to WANT, delflag is set to 1
2   3    0      delflag is still 1, hence obs. is not written to WANT
2   4    1      delflag is still 1, hence obs. is not written to WANT, delflag is set to 1```
🔒 This topic is solved and locked.