Hello,
I have a series of 10 Likert scale (1-3) variables that need to be recategorized using the same criteria. The variables are as follows:
a1_c_q1 - a1_c_q5, where 'a1' is administrator 1, 'c' is cognitive test, and 'q' represents the questionnaire number (1 though 5).
a2_c_q1 - a2_c_q5, where 'a2' is administrator 2, 'c ' is cognitive test, and 'q' represents the questionnaire number (1 through 5).
For each of the 10 variables, I am looking to create a new recategorized variable where 1=3 and 3=1. Is there a way to use an array or loop in the code so the same lines do not need to be repeated 10 times?
DATA new1;
SET new;
a1_c_q1_new=a1_c_q1;
IF a1_c_q1=3 THEN a1_c_q1_new=1;
ELSE IF a1_c_q1=3 THEN a1_c_q1_new=1;
RUN;
I think you need to look very closely that the IF that you show and see if that is correct:
IF a1_c_q1=3 THEN a1_c_q1_new=1;
ELSE IF a1_c_q1=3 THEN a1_c_q1_new=1;
I suspect that you meant
IF a1_c_q1=3 THEN a1_c_q1_new=1; ELSE IF a1_c_q1=1 THEN a1_c_q1_new=3;
I moved the code a bit to show that the IF and the ELSE are basically identical.
Here is a litteral translation of that into arrays:
DATA new1; SET new; array a (*) a1_c_q1 - a1_c_q5 a2_c_q1 - a2_c_q5 array new (*) newa1_c_q1 - newa1_c_q5 newa2_c_q1 - newa2_c_q5,; do i=1 to dim(a); new[i] = a[i]; if a[i]=3 then new[i]=1; else if a[i]=3 then new[i]=1; end; RUN;
Strongly recommend putting a suffix (start) on your new variables as you can then use list syntax as shown in the array definition. Otherwise you have to write out every single "_new" on the array definition and cannot use lists in any of the functions that might be useful to work with the values later.
Best might be to actually show a few values and the real result.
Another option could be a custom INFORMAT when reading text for those variables.
You can definitely do this, but you'll need to create two sets of arrays, one for the old variables and one for the new variables.
Note that I moved the _new to the middle of the questions to make it easier to shortcut the name references. Untested but this is the idea.
DATA new1;
SET new;
array _old (*) a1_c_q1 - a1_c_q5 a2_c_q1 - a2_c_q5;
array _new(*) a1_c_new_q1 - a1_c_new_q5 a2_c_new_q1 - a2_c_new_q5 ;
do i=1 to dim(_old);
_new(i) = _old(i)
IF _old(i)=3 THEN _new(i)=1;
ELSE IF _old(i)=1 THEN _old(i)=3;
end;
RUN;
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
@Adele3 wrote:
Hello,
I have a series of 10 Likert scale (1-3) variables that need to be recategorized using the same criteria. The variables are as follows:
a1_c_q1 - a1_c_q5, where 'a1' is administrator 1, 'c' is cognitive test, and 'q' represents the questionnaire number (1 though 5).
a2_c_q1 - a2_c_q5, where 'a2' is administrator 2, 'c ' is cognitive test, and 'q' represents the questionnaire number (1 through 5).
For each of the 10 variables, I am looking to create a new recategorized variable where 1=3 and 3=1. Is there a way to use an array or loop in the code so the same lines do not need to be repeated 10 times?
DATA new1; SET new; a1_c_q1_new=a1_c_q1; IF a1_c_q1=3 THEN a1_c_q1_new=1; ELSE IF a1_c_q1=3 THEN a1_c_q1_new=1; RUN;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.