Uttara
Uttara

Reputation: 125

giving the same value to a variable for the same group in SAS

I have a dataset that looks like this:

id    diagnosis
1        a
1        b
1        c
2        d
...

I'm trying to create a new variable called binary which will be assigned the value of 1 for every group of id for which one of entries for diagnosis is a. Right now my code looks somewhat like this:

data temp;
set a.dataset;
if diagnosis = a then binary = 1;
else binary = 0;
run;

The output for this looks like:

id    diagnosis   binary
1        a          1
1        b          0
1        c          0
2        d          0
...

In other words, I want the output to look like this instead:

id    diagnosis   binary
1        a          1
1        b          1
1        c          1
2        d          0
...

Can anybody please help?

Upvotes: 0

Views: 1802

Answers (2)

Amir
Amir

Reputation: 1000

I have assumed the initial data is sorted by id and I have set up some extra input records:

data have;
  input id $ diagnosis $;
  datalines;
1        a
1        b
1        c
2        d
2        e
3        f
3        a
;

A data step solution could use a merge, e.g.:

data want;
  merge have(in=in_h)
        have(in=in_h2 where=(diagnosis='a'));
  by id;

  binary=(in_h=in_h2);
run;

If the data is sorted by id and diagnosis then you could try:

data want;
  set have;
  by id;

  retain binary;

  if first.id then
    binary=(diagnosis='a');
run;

Upvotes: 1

Nguyễn Hải Triều
Nguyễn Hải Triều

Reputation: 1464

You can try this:

Result 1:

SELECT id, diagnosis,
       CASE diagnosis WHEN 'a' THEN 1 ELSE 0 END AS binary
FROM Your_Table

Result 2:

SELECT id, diagnosis,
       CASE id WHEN 1 THEN 1 ELSE 0 END AS binary
FROM Your_Table

Upvotes: 0

Related Questions