Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- make new variable that is true if any options in a group are true

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

☑ This topic is **solved**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 10-26-2022 03:54 PM
(467 views)

I have a feeling that I might be missing something simple, but I am having a difficult time figuring this out.

I have a dataset that looks something like this and what I am trying to do is create a new variable that is true if any of Var1 is true by each ID. For example I am for this new variable to be true for all 11 rows and 12 rows. I have been trying to make this work using a do loop, but have not been able to quite get that to work.

Data example

input ID Var1

Datalines;

11 1

11 0

11 0

11 0

12 1

12 1

12 1

12 0

13 0

13 0

14 0

;

RUN;

Essentially I am looking for an output that looks a bit like this

Data goal

input ID Var1 group

Datalines;

11 1 1

11 0 1

11 0 1

11 0 1

12 1 1

12 1 1

12 1 1

12 0 1

13 0 0

13 0 0

14 0 0

;

RUN;

Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

The MAX() of a set of boolean values is true when ANY of them is true.

```
proc sql;
create goal as
select * , max(var1) as group
from example
group by id
;
quit;
```

The MAX() of a set of boolean values is false when ALL of them are false.

The MIN() of a set of boolean values is true when ALL of them are true.

The MIN() of a set of boolean values is false when ANY of them are false.

The SUM() of a set of boolean values counts how many of them that are true.

The MEAN() of a set of boolean values is the percent of them that are true.

4 REPLIES 4

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

The MAX() of a set of boolean values is true when ANY of them is true.

```
proc sql;
create goal as
select * , max(var1) as group
from example
group by id
;
quit;
```

The MAX() of a set of boolean values is false when ALL of them are false.

The MIN() of a set of boolean values is true when ALL of them are true.

The MIN() of a set of boolean values is false when ANY of them are false.

The SUM() of a set of boolean values counts how many of them that are true.

The MEAN() of a set of boolean values is the percent of them that are true.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks so much for the help this worked perfectly!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Here is a do(w) loop approach

```
Data example;
input ID Var1;
Datalines;
11 1
11 0
11 0
11 0
12 1
12 1
12 1
12 0
13 0
13 0
14 0
;
data goal;
_iorc_ = 0;
do until (last.ID);
set example;
by ID;
if Var1 then _iorc_ = 1;
end;
do until (last.ID);
set example;
by ID;
group = _iorc_;
output;
end;
run;
```

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.