if q10_7= '1' then q10_7a=0;
if q10_7= '2' then q10_7a=1;
if q10_7= '3' then q10_7a=2;
if q10_7= '4' then q10_7a=3;
if q10_7= '5' then q10_7a=4;
if q10_8= '1' then q10_8a=0;
if q10_8= '2' then q10_8a=1;
if q10_8= '3' then q10_8a=2;
if q10_8= '4' then q10_8a=3;
if q10_8= '5' then q10_8a=4;
if q10_9= '1' then q10_9a=0;
if q10_9= '2' then q10_9a=1;
if q10_9= '3' then q10_9a=2;
if q10_9= '4' then q10_9a=3;
if q10_9= '5' then q10_9a=4;
if q10_4= '1' then q10_4a=0;
if q10_4= '2' then q10_4a=1;
if q10_4= '3' then q10_4a=2;
if q10_4= '4' then q10_4a=3;
if q10_4= '5' then q10_4a=4;
if q10_5= '1' then q10_5a=0;
if q10_5= '2' then q10_5a=1;
if q10_5= '3' then q10_5a=2;
if q10_5= '4' then q10_5a=3;
if q10_5= '5' then q10_5a=4;
if q10_6= '1' then q10_6a=0;
if q10_6= '2' then q10_6a=1;
if q10_6= '3' then q10_6a=2;
if q10_6= '4' then q10_6a=3;
if q10_6= '5' then q10_6a=4;
Dear programmers, is there a way i can make these code easier?
I am just trying to switch numbers to another...eg. "1" in original dataset to "0", "2" to "1" etc. I know i can do an "if..then" statement but that seems long.
Can anyone give me an idea how i can do this?
Thanks
One way:
q10_7a = input(q10_7, 1.) - 1;
q10_8a = input(q10_8, 1.) - 1;
You will need one statement for each group of statements.
This logic assumes that you want to do this for all possible values of the original variables, and they are all one digit long.
Depending on the number of questions that you need treated in this way, the code can be made shorter by using an array. But for that to happen, we might want to change the names of the new variables. Start here and see whether we're moving in the right direction.
One way:
q10_7a = input(q10_7, 1.) - 1;
q10_8a = input(q10_8, 1.) - 1;
You will need one statement for each group of statements.
This logic assumes that you want to do this for all possible values of the original variables, and they are all one digit long.
Depending on the number of questions that you need treated in this way, the code can be made shorter by using an array. But for that to happen, we might want to change the names of the new variables. Start here and see whether we're moving in the right direction.
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
You want to use arrays at minimum. I would also consider switching to an INFORMAT or FORMAT to recode the values rather than multiple IF/THEN but that's definitely an optional method.
data want;
set have;
array qs(*) q10_7 q10_8 q10_9 q10_4 ; *list all questions you want to recode here;
do i=1 to dim(qs);
if qs(i) = '1' then q10_9a=0;
else if qs(i) = '2' then q10_9a=1;
else if qs(i) = '3' then q10_9a=2;
else if qs(i) = '4' then q10_9a=3;
else if qs(i) = = '5' then q10_9a=4
end;
run;
@ChuksManuel wrote:
if q10_7= '1' then q10_7a=0; if q10_7= '2' then q10_7a=1; if q10_7= '3' then q10_7a=2; if q10_7= '4' then q10_7a=3; if q10_7= '5' then q10_7a=4; if q10_8= '1' then q10_8a=0; if q10_8= '2' then q10_8a=1; if q10_8= '3' then q10_8a=2; if q10_8= '4' then q10_8a=3; if q10_8= '5' then q10_8a=4; if q10_9= '1' then q10_9a=0; if q10_9= '2' then q10_9a=1; if q10_9= '3' then q10_9a=2; if q10_9= '4' then q10_9a=3; if q10_9= '5' then q10_9a=4; if q10_4= '1' then q10_4a=0; if q10_4= '2' then q10_4a=1; if q10_4= '3' then q10_4a=2; if q10_4= '4' then q10_4a=3; if q10_4= '5' then q10_4a=4; if q10_5= '1' then q10_5a=0; if q10_5= '2' then q10_5a=1; if q10_5= '3' then q10_5a=2; if q10_5= '4' then q10_5a=3; if q10_5= '5' then q10_5a=4; if q10_6= '1' then q10_6a=0; if q10_6= '2' then q10_6a=1; if q10_6= '3' then q10_6a=2; if q10_6= '4' then q10_6a=3; if q10_6= '5' then q10_6a=4;
Dear programmers, is there a way i can make these code easier?
I am just trying to switch numbers to another...eg. "1" in original dataset to "0", "2" to "1" etc. I know i can do an "if..then" statement but that seems long.
Can anyone give me an idea how i can do this?
Thanks
Like this?
data _F;
retain TYPE 'I' FMTNAME 'remap';
do START='1','2','3','4','5';
LABEL=input(START,1.)-1;
output;
end;
run;
proc format cntlin=_F; run;
data WANT;
Q10_7 = '1' ;
Q10_8 = '4' ;
Q10_7A=input(Q10_7,remap.);
Q10_8A=input(Q10_8,remap.);
run;
Arrays make the code harder to read. Only use them if you have a large number of variables.
Likewise, hard-code the proc format rather than use an input data set as I did, unless you have a large number of values to map.
Readability is always to be preferred.
What would be considered a large number of variables?
The best way to make your code simple is by transposing the unfavorable wide dataset into a long dataset.
See Maxim 33 and Maxim 19.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.