Solved
Contributor
Posts: 26

# how to add a variable to the data, if corresponding row have one of the values greater than a number

Hi i am stuck with basic prob.

I have two datasets Data1 having variables a, b, c, d like

a b c d

2 3 5 6

1 0 1 4

0 1 4 5

2 4 3 1

and Data2 having variables ch and lm

ch lm

p   3

q   5

l    7

m  4

I Need my output having variables ch (Data2), (a,b,c,d) from Data1 and a new variable "label" having "1" if any of the value in the row of data1 is >= 5.

my output data will have variables "ch", "a"," b", "c", "d", "label".

Thanks

Accepted Solutions
Solution
‎05-10-2017 06:35 AM
PROC Star
Posts: 1,817

## Re: how to add a variable to the data, if corresponding row have one of the values greater than a nu

I don't have a sound understanding of your req nor your data. Is this what you want?

data want;

merge data2(drop=lm) data1;

if max(a,b,c,d)>=5 then label=1;

run;

All Replies
Solution
‎05-10-2017 06:35 AM
PROC Star
Posts: 1,817

## Re: how to add a variable to the data, if corresponding row have one of the values greater than a nu

I don't have a sound understanding of your req nor your data. Is this what you want?

data want;

merge data2(drop=lm) data1;

if max(a,b,c,d)>=5 then label=1;

run;

Contributor
Posts: 26

## Re: how to add a variable to the data, if corresponding row have one of the values greater than a nu

Sir Thanks for your quick response,

Actually, It was a toy example.

Can you generalize if i have too many variables in Data1. So,  i can't put them manually.

Thanks

PROC Star
Posts: 1,817

## Re: how to add a variable to the data, if corresponding row have one of the values greater than a nu

You could use a variable list using -- (Double dash)

so for example: max(of a--d);

Super User
Posts: 6,778

## Re: how to add a variable to the data, if corresponding row have one of the values greater than a nu

[ Edited ]

Given that:

• You need to examine every numeric variable in Data1, and
• The new variable is based on variables in Data1 only,

You would be better off creating the new variable first, and combining the data sets later.  For example:

data data1_new;

set data1;

label = (max(of _numeric_) >= 5);

run;

Then do combine them, you can use either SET or MERGE.  It depends on what you want the result to be if there are different numbers of observations in DATA1 and DATA2.  Here is one possibility to consider:

data want;

set data1_new;

set data2 (drop=lm);

run;

Contributor
Posts: 26