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

Hello community.

 

I have a dataset with several (like 90) Likert scale variables, I want to transform them into numerical variables using a macro because each variable is an option of a question. I know I could do it with simple if/then/else but will take me forever and is prone to errors.

 

This is a sample of my data:

 

Scorpx_0-1609887456853.png

 

This is a sample of what I want that I did one by one:

if v7 in ("7.- Extremely important") then q1_1 = 7;
else if v7 in ("4.- Neutral") then q1_1=4;
else if v7 in ("1.- Not important at all") then q1_1=1;
else if v7 in ("2.-") then q1_1=2;
else if v7 in ("3.-") then q1_1=3;
else if v7 in ("5.-") then q1_1=5;
else q1_1=6;

if v8 in ("7.- Extremely important") then q1_2 = 7;
else if v8 in ("4.- Neutral") then q1_2=4;
else if v8 in ("1.- Not important at all") then q1_2=1;
else if v8 in ("2.-") then q1_2=2;
else if v8 in ("3.-") then q1_2=3;
else if v8 in ("5.-") then q1_2=5;
else q1_2=6;

This is the macro that I have now:

%MACRO Q1;
%DO I = 7 %TO 8;
%DO Z = 1 %TO 2;

%if V&I. ="7.- Extremely important" %then q1_&Z.=7;
%else %if V&I.="4.- Neutral" %then q1_&Z.=4;
%else %if V&I.="1.- Not important at all" %then q1_&Z.=1;
%else %if V&I.="2.-" %then q1_&Z.=2;
%else %if V&I.="3.-" %then q1_&Z.=3;
%else %if V&I.="5.-" %then q1_&Z.=5;
%else q1_&Z.=6;

%END;
%END;
%MEND Q1;

I doesn't create the new variables. I would appreciate any suggestion.

Thanks!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

IMO you need an array not a macro. 

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

array old_values(*) v1-v8;
array new_values(*) q1_1- q1_8;

do i=1 to dim(old_values);

if old_values(i) in ("7.- Extremely important") then new_values(i) = 7;
else if old_values(i)  in ("4.- Neutral") then new_values(i) 4;
else if old_values(i)  in ("1.- Not important at all") then new_values(i) =1;
else if old_values(i)  in ("2.-") then new_values(i) =2;
else if old_values(i)  in ("3.-") then new_values(i) =3;
else if old_values(i)  in ("5.-") then new_values(i) =5;
else new_values(i) =6;

end;

Or even better, add in a format.

https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf

 

Or since they all have the numbers in the text you could just keep just the numbers and remove all other characters using COMPRESS

do i=1 to dim(old_values);

new_values(i) = input(compress(old_values(i) , , 'kd'), 8.);

end;

@Scorpx wrote:

Hello community.

 

I have a dataset with several (like 90) Likert scale variables, I want to transform them into numerical variables using a macro because each variable is an option of a question. I know I could do it with simple if/then/else but will take me forever and is prone to errors.

 

This is a sample of my data:

 

Scorpx_0-1609887456853.png

 

This is a sample of what I want that I did one by one:

if v7 in ("7.- Extremely important") then q1_1 = 7;
else if v7 in ("4.- Neutral") then q1_1=4;
else if v7 in ("1.- Not important at all") then q1_1=1;
else if v7 in ("2.-") then q1_1=2;
else if v7 in ("3.-") then q1_1=3;
else if v7 in ("5.-") then q1_1=5;
else q1_1=6;

if v8 in ("7.- Extremely important") then q1_2 = 7;
else if v8 in ("4.- Neutral") then q1_2=4;
else if v8 in ("1.- Not important at all") then q1_2=1;
else if v8 in ("2.-") then q1_2=2;
else if v8 in ("3.-") then q1_2=3;
else if v8 in ("5.-") then q1_2=5;
else q1_2=6;

This is the macro that I have now:

%MACRO Q1;
%DO I = 7 %TO 8;
%DO Z = 1 %TO 2;

%if V&I. ="7.- Extremely important" %then q1_&Z.=7;
%else %if V&I.="4.- Neutral" %then q1_&Z.=4;
%else %if V&I.="1.- Not important at all" %then q1_&Z.=1;
%else %if V&I.="2.-" %then q1_&Z.=2;
%else %if V&I.="3.-" %then q1_&Z.=3;
%else %if V&I.="5.-" %then q1_&Z.=5;
%else q1_&Z.=6;

%END;
%END;
%MEND Q1;

I doesn't create the new variables. I would appreciate any suggestion.

Thanks!

 

 


 

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

Why did you change IF to %IF? and THEN into %THEN?

Looks like you just want to change V7 into V&I.

 

But why not just use arrays instead of resorting code generation?

Scorpx
Obsidian | Level 7
Thanks, Tom! but I didn't know how to do arrays, I tried with what I know. Evidently, the solution was easier than I thought.
Reeza
Super User

IMO you need an array not a macro. 

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

array old_values(*) v1-v8;
array new_values(*) q1_1- q1_8;

do i=1 to dim(old_values);

if old_values(i) in ("7.- Extremely important") then new_values(i) = 7;
else if old_values(i)  in ("4.- Neutral") then new_values(i) 4;
else if old_values(i)  in ("1.- Not important at all") then new_values(i) =1;
else if old_values(i)  in ("2.-") then new_values(i) =2;
else if old_values(i)  in ("3.-") then new_values(i) =3;
else if old_values(i)  in ("5.-") then new_values(i) =5;
else new_values(i) =6;

end;

Or even better, add in a format.

https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/001-30.pdf

 

Or since they all have the numbers in the text you could just keep just the numbers and remove all other characters using COMPRESS

do i=1 to dim(old_values);

new_values(i) = input(compress(old_values(i) , , 'kd'), 8.);

end;

@Scorpx wrote:

Hello community.

 

I have a dataset with several (like 90) Likert scale variables, I want to transform them into numerical variables using a macro because each variable is an option of a question. I know I could do it with simple if/then/else but will take me forever and is prone to errors.

 

This is a sample of my data:

 

Scorpx_0-1609887456853.png

 

This is a sample of what I want that I did one by one:

if v7 in ("7.- Extremely important") then q1_1 = 7;
else if v7 in ("4.- Neutral") then q1_1=4;
else if v7 in ("1.- Not important at all") then q1_1=1;
else if v7 in ("2.-") then q1_1=2;
else if v7 in ("3.-") then q1_1=3;
else if v7 in ("5.-") then q1_1=5;
else q1_1=6;

if v8 in ("7.- Extremely important") then q1_2 = 7;
else if v8 in ("4.- Neutral") then q1_2=4;
else if v8 in ("1.- Not important at all") then q1_2=1;
else if v8 in ("2.-") then q1_2=2;
else if v8 in ("3.-") then q1_2=3;
else if v8 in ("5.-") then q1_2=5;
else q1_2=6;

This is the macro that I have now:

%MACRO Q1;
%DO I = 7 %TO 8;
%DO Z = 1 %TO 2;

%if V&I. ="7.- Extremely important" %then q1_&Z.=7;
%else %if V&I.="4.- Neutral" %then q1_&Z.=4;
%else %if V&I.="1.- Not important at all" %then q1_&Z.=1;
%else %if V&I.="2.-" %then q1_&Z.=2;
%else %if V&I.="3.-" %then q1_&Z.=3;
%else %if V&I.="5.-" %then q1_&Z.=5;
%else q1_&Z.=6;

%END;
%END;
%MEND Q1;

I doesn't create the new variables. I would appreciate any suggestion.

Thanks!

 

 


 

Scorpx
Obsidian | Level 7
Thanks Reeza, I will accept your answer as the solution due to the examples you gave me. Evidently, the solution was easier than what I thought it should be.
ballardw
Super User

@Scorpx wrote:
Thanks Reeza, I will accept your answer as the solution due to the examples you gave me. Evidently, the solution was easier than what I thought it should be.

Large economy sized hint: When you start wanting to do the exact same thing to multiple variables some solution involves one or more arrays. There are may be other approaches but arrays are very powerful.

Tom
Super User Tom
Super User

Why all of the IF/THEN logic?

Why not just generate the value directly from the content?

q1_1 = input(V7,1.);
q1_2 = input(V8,1.);

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 849 views
  • 0 likes
  • 4 in conversation