BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Wafflecakes
Calcite | Level 5

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Assuming I understand correctly, this would do the trick:

 

data want;

set have;

if x=1 then x2=x1;

run;

View solution in original post

11 REPLIES 11
Astounding
PROC Star

It sounds like you are looking for this:

 

data want;

set have;

if x1=2 then x2=2;

run;

Wafflecakes
Calcite | Level 5
X1 equals values other than 2 like 350 so that wudnt work
Astounding
PROC Star

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;

Wafflecakes
Calcite | Level 5
I want x2 to be missing on purpose
Astounding
PROC Star

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.

Wafflecakes
Calcite | Level 5
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~=.
Astounding
PROC Star

Assuming I understand correctly, this would do the trick:

 

data want;

set have;

if x=1 then x2=x1;

run;

Wafflecakes
Calcite | Level 5
This is it. Such a simple step i made it so overly complicated.
ballardw
Super User

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
Calcite | Level 5
I only added the where statement so that the if statement executes in those places and the remainder is meant to be missing.
ballardw
Super User

@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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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