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

This is the data I have and want.

 

data want;
input ID visit question response v1_response;
cards;
1 1 1 4 4
1 2 1 3 4
1 1 2 6 6
1 2 2 9 6
;

Basically, I want to create variable v1_response, where it shows the visit 1 response by ID and question in order to calculate the change from visit 1 score for each question. I know how to do this when it is only 1 question in the dataset. But my approach is keeping only the value of the first question across all questions. 

 

This is my code so far

data have;
input ID visit question response;
cards;
1	1	1	4
1	2	1	3
1	1	2	6
1	2	2	9
;

proc sort data=have;
by ID visit question;
run;

data want;
set have;
by ID visit question;

retain v1_response;

if first.ID  AND first.question and visit=1 then do;
	v1_response=response;
end;
else if visit GT 1 do;
	change=response-v1_response;
end;

run;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data want;
input ID visit question response v1_response;
cards;
1 1 1 4 4
1 2 1 3 4
1 1 2 6 6
1 2 2 9 6
;

data want2;
 set want;
 by id question;
 retain want;
 if first.question then want=response;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20
data want;
input ID visit question response v1_response;
cards;
1 1 1 4 4
1 2 1 3 4
1 1 2 6 6
1 2 2 9 6
;

data want2;
 set want;
 by id question;
 retain want;
 if first.question then want=response;
run;
Tpham
Quartz | Level 8

THanks, this did work for me. So basically, i don't sort it by visit also it looks like.

 

However, how does SAS know, which is the first visit?

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 436 views
  • 0 likes
  • 3 in conversation