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

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.

 

Capture.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
Reeza
Super User
And then what? What do you want as output from this data? Are there multiple lines per StudyID or are you comparing answers between years for each question?

Please don't post data as pictures, we can't do much with them. A text data set is much easier to work with.
ballardw
Super User

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.

 

 

Kurt_Bremser
Super User

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;

sas-innovate-2024.png

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.

 

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
  • 3 replies
  • 421 views
  • 0 likes
  • 4 in conversation