14.5 - The IN= option

The IN= option tells SAS to create an "indicator variable" that takes on either the value 0 or 1 depending on whether or not the current observation comes from the input data set. If the observation does not come from the input data set, then the indicator variable takes on the value 0. If the observation does come from the input data set, then the indicator takes on the value 1. The IN= option is especially useful when merging and concatenating data sets which we'll study in the next two lessons. The basic format of the IN option is:

IN = varname

where varname is the name of a variable in the input data set.

Example 14.14 Section

The following program illustrates using the IN= option when concatenating — that is, appending one data set to another data set. Although we'll take a closer look at concatenating two or more data sets in the next lesson, this example will give you a taste of what's to come:

DATA back9;
	set temple okla (in=okie);
	if okie = 1 then hospital = 31;
		else if okie = 0 then hospital = 23;
RUN;
 
PROC PRINT data=back9;
	title 'Output Dataset: BACK9';
RUN;

Output Dataset: BACK9

Obs

subj

v_type

v_date

b_date

sex

state

country

hospital

1

230003

0

07/09/93

10/25/47

2

22

1

23

2

230004

0

01/04/94

08/15/23

2

38

1

23

3

230005

0

01/06/94

05/25/49

2

10

1

23

4

230006

0

01/06/94

04/24/49

2

21

1

23

5

230008

0

10/03/96

08/09/60

2

38

1

23

6

230009

0

10/31/96

11/13/50

1

38

1

23

7

310020

0

06/18/93

08/09/43

2

43

1

31

8

310032

0

08/03/93

04/02/34

2

13

1

31

9

310037

0

11/08/93

04/25/39

2

36

1

31

10

310041

0

10/27/93

11/01/26

2

43

1

31

11

310049

0

02/04/94

10/03/31

2

25

1

31

12

310055

0

07/12/94

12/11/56

2

36

1

31

13

310056

0

04/01/94

11/16/61

2

36

1

31

14

310059

0

06/13/94

08/22/34

2

43

1

31

15

310065

0

06/30/94

07/27/56

2

36

1

31

16

310069

0

09/23/94

07/14/42

2

36

1

31

17

310072

0

09/08/94

12/20/46

2

43

1

31

18

310073

0

09/26/94

02/27/33

2

36

1

31

19

310074

0

11/14/94

12/11/68

2

5

1

31

20

310080

0

12/16/94

03/14/57

2

36

1

31

21

310082

0

01/17/95

09/15/45

2

4

1

31

22

310083

0

01/20/95

05/13/54

1

.

17

31

23

310084

0

01/20/95

09/29/60

2

36

1

31

24

310085

0

09/01/95

08/08/66

2

16

1

31

25

310087

0

09/27/95

02/27/29

2

36

1

31

26

310088

0

09/14/95

07/05/53

2

27

1

31

27

310089

0

09/14/95

04/07/53

2

36

1

31

28

310090

0

01/31/96

03/28/52

2

35

1

31

29

310091

0

01/16/96

10/07/61

2

36

1

31

30

310092

0

01/05/96

07/12/39

1

36

1

31

31

310094

0

12/15/95

07/25/60

2

36

1

31

32

310096

0

12/06/95

05/23/27

2

36

1

31

33

310097

0

12/18/95

10/13/23

1

36

1

31

34

310100

0

02/23/96

06/03/27

2

36

1

31

35

310102

0

02/12/96

05/06/18

2

1

1

31

36

310103

0

02/12/96

09/16/34

2

4

1

31

37

310104

0

02/13/96

02/11/44

2

35

1

31

38

310106

0

02/15/96

11/24/56

2

36

1

31

39

310107

0

03/05/96

11/10/39

2

13

1

31

40

310108

0

02/21/96

12/29/45

2

.

4

31

41

310109

0

05/08/96

04/15/61

2

36

1

31

42

310110

0

05/14/96

12/23/46

2

5

1

31

43

310111

0

03/21/96

01/10/62

2

36

1

31

44

310112

0

03/25/96

07/08/44

2

36

1

31

45

310113

0

05/22/96

01/12/47

1

36

1

31

46

310114

0

05/23/96

02/04/38

1

36

1

31

47

310115

0

05/28/96

05/29/69

2

36

1

31

48

310117

0

06/06/96

06/15/47

2

5

1

31

49

310120

0

09/18/96

10/20/69

2

5

1

31

50

310121

0

07/19/96

10/23/39

2

43

1

31

51

310122

0

07/12/96

07/15/47

1

43

1

31

52

310123

0

07/23/96

06/13/45

2

14

1

31

53

310124

0

07/31/96

04/20/51

2

36

1

31

54

310125

0

08/13/96

04/13/31

2

4

1

31

55

310126

0

09/18/96

02/15/68

2

36

1

31

56

310128

0

10/30/96

10/26/20

2

36

1

31

57

310130

0

10/16/96

06/16/59

2

31

1

31

58

310132

0

11/04/96

06/15/70

2

43

1

31

The SET statement, in which both data set names temple and okla appear, tells SAS to concatenate the two data sets named temple and okla. That is, SAS will append the data set okla to the data set temple, so that the temporary data set back9 will contain 58 observations — 6 observations from temple and 52 observations from okla for a total of 58 observations.

The IN= option here tells SAS to create a temporary variable called okie that takes on the value 1 if the observation came from the okla data set and 0 if it did not. Therefore, the variable okie will equal 1 for the 52 observations from the data set okla and will equal 0 for the 6 observations from the data set temple. Because the indicator variable created by the IN= option is temporary, it goes away as soon as you leave the DATA step. For example, you can not print the indicator variable. To get around this, you can use the temporary variable to create a permanent variable. In this program, the temporary variable okie is used to create the permanent variable hospital.

Now, launch and run  the SAS program. Review the output from the PRINT procedure. Convince yourself that the temporary data set back9 does indeed contain 58 observations — 6 observations from temple and 52 observations from okla. Also, verify that the variable hospital was created as expected from the temporary variable okie.

Example 14.15 Section

The following program illustrates a cute programming trick when using the IN= option. Specifically, it illustrates how SAS assumes that you mean "if varname = 1" in an IF statement if you just say "if varname" where varname is the variable name specified in an IN= option. Therefore, you can use this fact to create helpful temporary variable names, such as indatasetname. Let's take a look:

DATA back10;
    set temple okla (in=inokie);
    if inokie then hospital = 31;
        else hospital = 23;
RUN;
 
PROC PRINT data=back10;
    title 'Output Dataset: BACK10';
RUN;

Output Dataset: BACK10

Obs

subj

v_type

v_date

b_date

sex

state

country

hospital

1

230003

0

07/09/93

10/25/47

2

22

1

23

2

230004

0

01/04/94

08/15/23

2

38

1

23

3

230005

0

01/06/94

05/25/49

2

10

1

23

4

230006

0

01/06/94

04/24/49

2

21

1

23

5

230008

0

10/03/96

08/09/60

2

38

1

23

6

230009

0

10/31/96

11/13/50

1

38

1

23

7

310020

0

06/18/93

08/09/43

2

43

1

31

8

310032

0

08/03/93

04/02/34

2

13

1

31

9

310037

0

11/08/93

04/25/39

2

36

1

31

10

310041

0

10/27/93

11/01/26

2

43

1

31

11

310049

0

02/04/94

10/03/31

2

25

1

31

12

310055

0

07/12/94

12/11/56

2

36

1

31

13

310056

0

04/01/94

11/16/61

2

36

1

31

14

310059

0

06/13/94

08/22/34

2

43

1

31

15

310065

0

06/30/94

07/27/56

2

36

1

31

16

310069

0

09/23/94

07/14/42

2

36

1

31

17

310072

0

09/08/94

12/20/46

2

43

1

31

18

310073

0

09/26/94

02/27/33

2

36

1

31

19

310074

0

11/14/94

12/11/68

2

5

1

31

20

310080

0

12/16/94

03/14/57

2

36

1

31

21

310082

0

01/17/95

09/15/45

2

4

1

31

22

310083

0

01/20/95

05/13/54

1

.

17

31

23

310084

0

01/20/95

09/29/60

2

36

1

31

24

310085

0

09/01/95

08/08/66

2

16

1

31

25

310087

0

09/27/95

02/27/29

2

36

1

31

26

310088

0

09/14/95

07/05/53

2

27

1

31

27

310089

0

09/14/95

04/07/53

2

36

1

31

28

310090

0

01/31/96

03/28/52

2

35

1

31

29

310091

0

01/16/96

10/07/61

2

36

1

31

30

310092

0

01/05/96

07/12/39

1

36

1

31

31

310094

0

12/15/95

07/25/60

2

36

1

31

32

310096

0

12/06/95

05/23/27

2

36

1

31

33

310097

0

12/18/95

10/13/23

1

36

1

31

34

310100

0

02/23/96

06/03/27

2

36

1

31

35

310102

0

02/12/96

05/06/18

2

1

1

31

36

310103

0

02/12/96

09/16/34

2

4

1

31

37

310104

0

02/13/96

02/11/44

2

35

1

31

38

310106

0

02/15/96

11/24/56

2

36

1

31

39

310107

0

03/05/96

11/10/39

2

13

1

31

40

310108

0

02/21/96

12/29/45

2

.

4

31

41

310109

0

05/08/96

04/15/61

2

36

1

31

42

310110

0

05/14/96

12/23/46

2

5

1

31

43

310111

0

03/21/96

01/10/62

2

36

1

31

44

310112

0

03/25/96

07/08/44

2

36

1

31

45

310113

0

05/22/96

01/12/47

1

36

1

31

46

310114

0

05/23/96

02/04/38

1

36

1

31

47

310115

0

05/28/96

05/29/69

2

36

1

31

48

310117

0

06/06/96

06/15/47

2

5

1

31

49

310120

0

09/18/96

10/20/69

2

5

1

31

50

310121

0

07/19/96

10/23/39

2

43

1

31

51

310122

0

07/12/96

07/15/47

1

43

1

31

52

310123

0

07/23/96

06/13/45

2

14

1

31

53

310124

0

07/31/96

04/20/51

2

36

1

31

54

310125

0

08/13/96

04/13/31

2

4

1

31

55

310126

0

09/18/96

02/15/68

2

36

1

31

56

310128

0

10/30/96

10/26/20

2

36

1

31

57

310130

0

10/16/96

06/16/59

2

31

1

31

58

310132

0

11/04/96

06/15/70

2

43

1

31

Do you get it? The temporary variable inokie equals 1 for records coming from the okla data set and 0 for records coming from the temple data set. The IF statement does not (or need not) say "if inokie = 1 then hospital = 31." Instead, the IF statement says the much more English-sounding "if inokie then hospital = 31."

Launch and run  the SAS program. Review the output from the PRINT procedure, and convince yourself that the back10 data set has the same structure and contents as the back9 data set from the previous example.