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
- /
- Base SAS Programming
- /
- Missing Data

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

10-10-2017 09:55 AM - edited 10-10-2017 10:35 AM

To SAS experts - I have repeated measures data (multiple obs per id) and I am trying to duplicate a variable but only keep values that satisfy a condition:

Have

id x x1

1 1 2

1 2 .

1 2 30

2 1 350

2 2 .

2 2 30

Want

id x x1 x2

1 1 2 2

1 2 . .

1 2 30 .

2 1 350 350

2 2 . .

2 2 30 .

I tried this :

data try;

set try;

if x1~=. then x2=x1;

where x1=2;

run;

SAS deletes all of my other observations and only keeps those that follow the if statement - what do I do?

Accepted Solutions

Solution

10-10-2017
10:50 AM

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

Posted in reply to Wafflecakes

10-10-2017 10:47 AM

Assuming I understand correctly, this would do the trick:

data want;

set have;

if x=1 then x2=x1;

run;

All Replies

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

Posted in reply to Wafflecakes

10-10-2017 09:59 AM

It sounds like you are looking for this:

data want;

set have;

if x1=2 then x2=2;

run;

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

Posted in reply to Wafflecakes

10-10-2017 10:06 AM

X1 equals values other than 2 like 350 so that wudnt work

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

Posted in reply to Wafflecakes

10-10-2017 10:12 AM

You may need to explain a fuller set of rules then. Take the example in your data where X1 is 3. Why is X2 missing, and not 3?

Could the solution be as simple as this?

x2 = x1;

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

Posted in reply to Wafflecakes

10-10-2017 10:21 AM

I want x2 to be missing on purpose

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

Posted in reply to Wafflecakes

10-10-2017 10:25 AM

OK, time for full disclosure then. No secrets allowed.

When should X2 be equal to X1?

When should X2 be missing?

Are there any other possibilities?

The programming is easy. You just have to spell out the rules.

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

Posted in reply to Astounding

10-10-2017 10:36 AM

I modified the original post. Basically you will notice that x can equal 1 or 2. I only want to keep the values of x1 for when x=1 otherwise x2=. even if x1~=.

Solution

10-10-2017
10:50 AM

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

Posted in reply to Wafflecakes

10-10-2017 10:47 AM

Assuming I understand correctly, this would do the trick:

data want;

set have;

if x=1 then x2=x1;

run;

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

Posted in reply to Astounding

10-10-2017 10:50 AM

This is it. Such a simple step i made it so overly complicated.

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

Posted in reply to Wafflecakes

10-10-2017 10:24 AM

The purpose of this statement is to remove any observation where the value of X1 is other than 2

where x1=2;

So you told SAS to delete observations.

Provide a description of the actual rule(s) involved.

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

Posted in reply to ballardw

10-10-2017 10:40 AM

I only added the where statement so that the if statement executes in those places and the remainder is meant to be missing.

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

Posted in reply to Wafflecakes

10-10-2017 04:02 PM

Wafflecakes wrote:

I only added the where statement so that the if statement executes in those places and the remainder is meant to be missing.

Where does not work that way. Where subsets data given the rules on the statement. It actually works as data is read and does not matter where in the data step the where statement exists. Unless you have multiple where statements, in which case the last one is likely going to be the one in effect.