Solved
Contributor
Posts: 51

# Missing Data

[ Edited ]

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
Super User
Posts: 6,214

## Re: Missing Data

Posted in reply to Wafflecakes

Assuming I understand correctly, this would do the trick:

data want;

set have;

if x=1 then x2=x1;

run;

All Replies
Super User
Posts: 6,214

## Re: Missing Data

Posted in reply to Wafflecakes

It sounds like you are looking for this:

data want;

set have;

if x1=2 then x2=2;

run;

Contributor
Posts: 51

## Re: Missing Data

Posted in reply to Wafflecakes
X1 equals values other than 2 like 350 so that wudnt work
Super User
Posts: 6,214

## Re: Missing Data

Posted in reply to Wafflecakes

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;

Contributor
Posts: 51

## Re: Missing Data

Posted in reply to Wafflecakes
I want x2 to be missing on purpose
Super User
Posts: 6,214

## Re: Missing Data

Posted in reply to Wafflecakes

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.

Contributor
Posts: 51

## Re: Missing Data

Posted in reply to Astounding
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
Super User
Posts: 6,214

## Re: Missing Data

Posted in reply to Wafflecakes

Assuming I understand correctly, this would do the trick:

data want;

set have;

if x=1 then x2=x1;

run;

Contributor
Posts: 51

## Re: Missing Data

Posted in reply to Astounding
This is it. Such a simple step i made it so overly complicated.
Super User
Posts: 12,394

## Re: Missing Data

Posted in reply to Wafflecakes

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.

Contributor
Posts: 51

## Re: Missing Data

I only added the where statement so that the if statement executes in those places and the remainder is meant to be missing.
Super User
Posts: 12,394

## Re: Missing Data

Posted in reply to Wafflecakes

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 11 replies
• 226 views
• 0 likes
• 3 in conversation