Help using Base SAS procedures

How to contrast values of two variables

Reply
Frequent Contributor
Posts: 75

How to contrast values of two variables

The data look like this:

data temp;

input Var1 Var2;

datalines;

5 .

7 .

8 .

2 1

3 1

1 2

1 4

4 5

6 5

6 7

6 8

;

run;

The idea is: there are cases where values of Var1 are equal to values of Var2 elsewhere while the corresponding values of Var2 are missing (values 5, 7, 8). The other specifications are not important I think.

How can I select values of Var2 as specified above (5, 7, 8) and turn them into missing (or whatever value)? That way it looks like:

5 .

7 .

8 .

2 1

3 1

1 2

1 4

4 .

6 .

6 .

6 .

Respected Advisor
Posts: 4,661

Re: How to contrast values of two variables

Like this:

proc sql;

create table want as

select

     a.var1,

     case

          when missing(b.var1) then a.var2

          else b.var2

          end as var2

from

     temp as a left join

     temp as b on a.var2=b.var1;

quit;

PG

PG
Frequent Contributor
Posts: 120

Re: How to contrast values of two variables

Something like this.....

proc sql;
select distinct var2 into :var2values separated by ' '
from temp
where var2 ne .
;

select var1 into :missvar separated by ' '
from temp
where var2 eq . and var1 in (&var2values)
;
quit;

data out;
set temp;
if var2 in (&missvar) then var2 = .;
run;

//Fredrik

Ask a Question
Discussion stats
  • 2 replies
  • 153 views
  • 0 likes
  • 3 in conversation