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?

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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