Turn on suggestions

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

Showing results for

Options

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

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

Posted 03-22-2021 09:10 PM
(8215 views)

Hello,

I have some data and want to create a new variable from my current variables. I basically want the following outcome:

IF var1 = 0 and var2 = 0 then newvariable = 1

If var1 =0 and var2 = 1 then newvariable=2

If var1 = 1 and var2=0 then newvariable = 3

if var1 = 1 and var2=1 then newvariable = 4

I have tried the following and it has not worked

Data new;

set old;

IF var1 = 0 and var2 = 0 then newvariable = 1;

else If var1 =0 and var2 = 1 then newvariable=2;

else If var1 = 1 and var2=0 then newvariable = 3;

else if var1 = 1 and var2=1 then newvariable = 4;

RUN;

Any help would be greatly appreciated!

7 REPLIES 7

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

What is the input?

What is the output?

Your if statements look ok.

FYI all you need is a single assignment instead of 4 ifs:

newvariable = 2 * var1 + var2 + 1;

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

I've attached an example of my input (i've tried with and without parentheses)

When I print the new variable "group8", I would expect to see it coded 1,2,3 or 4 for each subject, but it is just blank

At the end of this I basically want a new variable "group8" coded so I have 4 levels based on those original variables:

treatment 1 + diagnosis 1 = 1

treatment 2 + diagnosis 1 = 2

treatment 1 + diagnosis 2 = 3

treatment 2 + diagnosis 2 = 4

Hope this makes sense. Thank you for your help.

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

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

No, group8 is an arbitrary variable name I am trying to create based on these diagnosis and treatment variables. It does not exist in the input data set.

The input for the other variables is something like this -

What I want is SAS to code my data based on these variables so it puts out something like this:

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

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

Here is a sample data set and solution. It works for me. Try it out:

```
data run1;
input subject treatment diagnosis;
datalines;
1 0 1
2 1 0
3 0 0
4 0 1
5 1 0
6 1 1
7 0 1
8 1 0
9 0 0
;
data newrun1;
set run1;
if treatment=0 and diagnosis=0 then group8=1;
else if treatment=0 and diagnosis=1 then group8=2;
else if treatment=1 and diagnosis=0 then group8=3;
else if treatment=1 and diagnosis=1 then group8=4;
run;
proc print; run;
```

Most likely, the problem is your data. Either your input data set doesn't have the variables Treatment and Diagnosis (check your spelling!) or those variables do not have the values you think they do. Use the following to examine the values of your variables:

```
proc freq data=run1;
tables treatment * diagnosis / norow nocol nopercent;
run;
```

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

@Rose1 wrote:

...

When I print the new variable "group8", I would expect to see it coded 1,2,3 or 4 for each subject, but it is just blank

Check the log! Are there any unexpected notes, warnings?

**SAS Innovate 2025** is scheduled for May 6-9 in Orlando, FL. Sign up to be **first to learn** about the agenda and registration!

Upcoming Events

- VALSUG User Group Meeting | 07-Nov-2024
- Ask the Expert: How Do I Perform Customer Segmentation With SAS Intelligent Decisioning? | 12-Nov-2024
- Ask the Expert: How Do I Perform Customer Segmentation With SAS Intelligent Decisioning? | 12-Nov-2024
- Club SAS de Quebec | 13-Nov-2024
- Hands-on Workshop: SAS® Viya® Workbench (at SAS Headquarters) | 13-Nov-2024
- SAS Bowl XLV, SAS Visual Analytics | 13-Nov-2024
- Ask the Expert: Leveraging R for Statistical Analysis in LSAF | 19-Nov-2024

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.