This is a answer from Learning SAS® by Example: A Programmer's Guide, Second Edition. But I think the rusults are not the what expected to get.
Q: Using the permanent SAS data set Survey1, create a new, temporary SAS data set (Survey1)
where the values of the variables Ques1–Ques5 are reversed as follows:
1 5; 2 4; 3 3; 4 2; 5 1.
*13-1;
data survey1;
set learn.survey1;
array Ques{5} $ Q1-Q5;
do i = 1 to 5;
Ques{i} = translate(Ques{i},'54321','12345');
end;
drop i;
run;
title "List of SURVEY1 (rescaled)";
proc print data=learn.survey1;
run;
proc print data=survey1;
run;
List of SURVEY1 (rescaled)
Obs Subj Q1 Q2 Q3 Q4 Q512345
001 | 1 | 3 | 5 | 4 | 2 |
002 | 5 | 5 | 4 | 4 | 3 |
003 | 2 | 1 | 2 | 1 | 1 |
004 | 3 | 5 | 1 | 4 | 2 |
005 | 3 | 3 | 3 | 3 | 3 |
List of SURVEY1 (rescaled)
Obs Subj Q1 Q2 Q3 Q4 Q512345
001 | 5 | 3 | 1 | 2 | 4 |
002 | 1 | 1 | 2 | 2 | 3 |
003 | 4 | 5 | 4 | 5 | 5 |
004 | 3 | 1 | 5 | 2 | 4 |
005 | 3 | 3 | 3 | 3 | 3 |
The first table is the original dataset
the second is after transformation
Where specifically do you have a question? Which specific values? Which observation (row)?
We don't have that data set so you need to be very explicit about your concern. From what I see the second output has all of the 1 changed to 5 and vice versa and 2 changed to 4 and vice versa. Isn't that what the introduction says to do?
(Not the way I would do this and actually not needed at all for 99% of activitiy, but these are learning examples)
You really need to provide two different TITLES for the two proc prints because your title implies that they are the same output but it isn't. One of the titles should say something about "before transformation" and the other "after transformation".
The first table is the original dataset
the second is after transformation
the old obs1:
001 | 1 | 3 | 5 | 4 | 2 |
the new dataset, should it be:
001 | 2 | 4 | 5 | 3 | 1 |
thanks
@jz127323 wrote:
the old obs1:
001 1 3 5 4 2
the new dataset, should it be:
001 2 4 5 3 1
thanks
Where is the problem? The new dataset is correct.
Note: I compared the original datasets, not the obs posted here.
@jz127323 you may want to show the code you have written, as text using "insert sas code" button, so that we can actually help.
@jz127323 wrote:
the old obs1:
001 1 3 5 4 2
the new dataset, should it be:
001 2 4 5 3 1
thanks
No. From your description, 1 changes to 5 and 2 to 4 (and vice versa), while 3 remains unchanged.
@jz127323 wrote:
the old obs1:
001 1 3 5 4 2
the new dataset, should it be:
001 2 4 5 3 1
thanks
No, reverse coding means reverse the values individually so that :
5 -> 1
4 -> 2
3 -> 3
2 -> 4
1 -> 5
You seem to want to reverse the order of the values in the row, ie what is in col5 goes to col1 which is not the correct interpretation of the question.
Thanks for everyone's help. I totally misunderstood the meaning of the question. Not to exchange the values of columns, just change the value itselves.
Thanks again!
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!
Ready to level-up your skills? Choose your own adventure.