## Comparing Variables that are in different Data Sets

Solved
Occasional Contributor
Posts: 14

# Comparing Variables that are in different Data Sets

Suppose 3 data sets with the names A, B, and C, which had been previously created.
The Data Set A contains only the variable named A1 with several observations.
The Data Set C contains only the variable named C1 with several observations.
The Data Set B has 2 variables, B1 and B2, but only with 2 observations.

 Data Set A B C Result for D Variables A1 B1 B2 C1 D1 D2 Observation 1 4 -3 8 -2 4 -0.15 Observation 2 -45 -6 19 -3 -0.23 Observation 3 23 -5 -0.38 Observation 4 1 5 1 0.38 Observation 5 2 9 2 0.69 Observation 6 -56 -13 -1 Observation 7 -4 1 0.07 Observation 8 . . . . Observation 9 . . . . Observation 10 . . . .

1- I want to compare if [A1 (all observations)] is greater than ([B1 - observation 1] = -3) & less than ([B2 - observation 1] = 8). If so, save the values ​​into a new Data Set, named  D, as variable D1 (A1 values ​​that do not satisfy the conditions must be empty in D1).

2- I want to divide all the observations of C1 per (B1 + B2 -> observation 2 ->  ((-6)+(19)) = 13. Save the result as variable D2 on Data Set D.

Accepted Solutions
Solution
‎07-02-2013 08:19 AM
Super Contributor
Posts: 340

## Re: Comparing Variables that are in different Data Sets

Hi,

Does the following give you what you want?:

data a;

input a1;

datalines;

4

-45

23

1

2

-56

-4

.

.

.

;

data b;

input b1 b2;

datalines;

-3 8

-6 19

;

data c;

input c1;

datalines;

-2

-3

-5

5

9

-13

1

.

.

.

;

data _null_;

set b;

if _n_=1 then

do;

call symputx('b1_1',b1);

call symputx('b1_2',b2);

end;

else

call symputx('b2sum',b1+b2);

run;

data d(keep=d1 d2);

set a;

d1=ifn(a1 gt &b1_1 and a1 lt &b1_2,a1,.);

set c;

d2=round(c1/&b2sum,0.01);

run;

Regards,

Amir

Message was edited by: Amir Malik - changed some formatting.

All Replies
Solution
‎07-02-2013 08:19 AM
Super Contributor
Posts: 340

## Re: Comparing Variables that are in different Data Sets

Hi,

Does the following give you what you want?:

data a;

input a1;

datalines;

4

-45

23

1

2

-56

-4

.

.

.

;

data b;

input b1 b2;

datalines;

-3 8

-6 19

;

data c;

input c1;

datalines;

-2

-3

-5

5

9

-13

1

.

.

.

;

data _null_;

set b;

if _n_=1 then

do;

call symputx('b1_1',b1);

call symputx('b1_2',b2);

end;

else

call symputx('b2sum',b1+b2);

run;

data d(keep=d1 d2);

set a;

d1=ifn(a1 gt &b1_1 and a1 lt &b1_2,a1,.);

set c;

d2=round(c1/&b2sum,0.01);

run;

Regards,

Amir

Message was edited by: Amir Malik - changed some formatting.

🔒 This topic is solved and locked.