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;
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;
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;
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?
Because of the by statement, SAS keeps track of the by variables, and sets the first. and last. variables at every group change.
Keep the order as is, run the data step with
by id question;
and set v1_response at first.question.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.