turn on suggestions

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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Splitting one variable into multiple based on crit...

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-02-2016 10:42 AM

Hi,

I have a question which is intuitively not very difficult, but I cannot seem to find a way to do this in SAS.

I have two variables: variable X (categorical: levels A and B), and a continious variable Z (numeric).

I want to create two new variables out of variable Z, with values that depend on the level of X (A or B).

See the accompanying table. Someone knows the appropriate syntax? Thanks a lot in advance!

X | Z | Z1 | Z2 |

A | 4.2 | 4.2 | |

A | 3 | 3 | |

B | 7 | 7 | |

A | 5 | 5 | |

B | 8 | 8 | |

B | 8.5 | 8.5 |

Accepted Solutions

Solution

11-04-2016
06:17 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-02-2016 10:55 AM

I understand that Z1 and Z2 are two new variables to create:

Data want;

set have;

if x='A' then Z1=Z; else

if x='B' then Z2=Z;

run;

All Replies

Solution

11-04-2016
06:17 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-02-2016 10:55 AM

I understand that Z1 and Z2 are two new variables to create:

Data want;

set have;

if x='A' then Z1=Z; else

if x='B' then Z2=Z;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-02-2016 10:56 AM

It looks like you want something like:

data want;

set have;

if X = 'A' Then Z1 = z;

else if X='B' then Z2 = z;

run;

Note: If you have a large number of values of X to consider there are different approaches that may work such as use of the IN comparison if the same thing is done for multiple values or a different structure called SELECT if you have multiple values and each generates a different output.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-02-2016 11:04 AM

```
data have;
input X $ Z;
datalines;
A 4.
A 3
B 7
A 5
B 8
B 8.5
;
Data want;
set have;
if x='A' then Z1=Z;
else x='B' then Z2=Z;
run;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-02-2016 11:28 AM

To me, the quesiton is why. Currently you have a dataset with two columns, one indicates data position (i.e a where=a would show all z1 for instance), and one is the result. There are no missing elements. What you are asking for is a dataset which is twice as big - 4 variables - with various missing elements, but doesn't actually add anything to the data. So why bother, its just taking more room but without any benefit.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-04-2016 06:18 AM

I understand your point. The reason I want to do it, is because they should really represent two distinct variables. Therefore, I really prefer to have them in seperate columns, rather than using the BY statement all the time. Or maybe my mind is rather unlogical...