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?
Assuming I understand correctly, this would do the trick:
data want;
set have;
if x=1 then x2=x1;
run;
It sounds like you are looking for this:
data want;
set have;
if x1=2 then x2=2;
run;
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;
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.
Assuming I understand correctly, this would do the trick:
data want;
set have;
if x=1 then x2=x1;
run;
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.
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.