I am trying to compare the results from one column to others.
For example, I am looking at Q11 and want to know if, throughout the years, the answer for each study_id went from good to bad, bad to good, or stayed the same. So I am trying to compare the earlier columns to the later ones. As you keep moving down the list, you will encounter other questions (Q12, Q13, Q26, Q31, etc.). Attached is a picture of some of the data.
Transpose, then analyze:
proc transpose
data=have
out=trans (
rename=(_name_=period col1=answer)
where=(answer ne ' ')
)
;
by study_id new_q;
var ans:;
run;
data want;
set trans;
by study_id new_q;
prev_ans = lag(answer);
length result $20;
if not first.new_q
then do;
if prev_ans = answer then result = 'same';
else if prev_ans = 'bad' then result = 'bad to good';
else result = 'good to bad';
end;
run;
Do you more than 2 values for any of the ans2010 to 2019?
What if you have a sequence like "good", "bad", "good"? what is the result?
Or "good" "good" "bad" "bad" "bad" "bad" "good"?
Or what if only one of the ans2010 to ans2019 has any value?
You have apparently 10 possible answers with LOTS of possible combinations: 3**10 actually (good, bad or missing for each). So your desired result really kind of depends on
I have a sneaking suspicion that this data would benefit from coding "good" to a numeric 1 and "bad" to a zero, which would require new variables. At least
r= range(of num_ans:);
g= sum(of num_ans:);
b= n(of num_ans:) - g;
m = mean(of num_ans:);
if the numeric coded variables were named Num_ans_2010 to Num_ans_2019:
if r=0 then all are the same.
g = the number of "good";
b = the number of "bad";
m= percentage of "good" among the answers.
for each row.
Transpose, then analyze:
proc transpose
data=have
out=trans (
rename=(_name_=period col1=answer)
where=(answer ne ' ')
)
;
by study_id new_q;
var ans:;
run;
data want;
set trans;
by study_id new_q;
prev_ans = lag(answer);
length result $20;
if not first.new_q
then do;
if prev_ans = answer then result = 'same';
else if prev_ans = 'bad' then result = 'bad to good';
else result = 'good to bad';
end;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.