Transforming 4 pt likert into distinct binary

Solved
Occasional Contributor
Posts: 7

Transforming 4 pt likert into distinct binary

I ran a proc contents with an out= to create a dataset that has all of the variables in a single column.  From here I want to be able to perform a handful of transformations based on the middle position of said variables, they look like this:

tml021success

tml021datainform

...there are approx 100 of these vars, and I only want to transform into new vars those vars that have an '046' or '021' as their substr(name,4,3)

the transformation is to make tml021success (1 thru 2=0)  (3 thru 4 = 1) 5=sysmis into ra_tml021success.

questions is:  What proc do I use (or macro) to make the program loop through and identify those vars with a specific substring and recode them into newly created vars?

Any help much appreciated.

Novice sas user

Accepted Solutions
Solution
‎09-06-2013 03:52 PM
Super User
Posts: 7,932

Re: Transforming 4 pt likert into distinct binary

You have two questions here.

1) How to recode one of these variables.

2) How to simplify identifying all of them and generating the code to re code them.

One way to generate the list of variable names needed to define an array is to use PROC SQL to make a macro variable with the space delimited list of variable names.

proc sql noprint ;

select name , 'ra_'||name

into : original separated by ' '

, : recoded separated by ' '

from contents

where substr(name,4,3) in ('021','046')

;

quit;

One simple way to apply the same logic to multiple variables is to use an ARRAY ( or in this case two synchronized arrays).

data want ;

set have ;

array _a &original;

array _b &recoded ;

do over _a ;

if 1 <= _a <= 2 then _b = 0 ;

else if 3 <= _a <= 4 then _b=1 ;

else _b=.;

end;

run;

All Replies
Super User
Posts: 13,304

Re: Transforming 4 pt likert into distinct binary

You don't have to modify any data for most purposes. A custom format such as

Proc format library=work;

value MyBin

1,2 = '0' /* or you can use False, No or whatever 0 actually means to you in place of 0*/

3,4 = '1' /* or True, Yes or whatever*/

5= .

;

run;

Then in analysis procedures or any display associate the format with the variable.

proc freq data=yourdatasetname;

tables tml021success;

format tml021success MyBin. ;

run;

One advantage of this approach is that you can have multiple formats that can be assigned to the same variables for different types of analysis and don't have to create multiple variables such as if you wanted to see how groups based on 1, 2 and 3, 4.

Solution
‎09-06-2013 03:52 PM
Super User
Posts: 7,932

Re: Transforming 4 pt likert into distinct binary

You have two questions here.

1) How to recode one of these variables.

2) How to simplify identifying all of them and generating the code to re code them.

One way to generate the list of variable names needed to define an array is to use PROC SQL to make a macro variable with the space delimited list of variable names.

proc sql noprint ;

select name , 'ra_'||name

into : original separated by ' '

, : recoded separated by ' '

from contents

where substr(name,4,3) in ('021','046')

;

quit;

One simple way to apply the same logic to multiple variables is to use an ARRAY ( or in this case two synchronized arrays).

data want ;

set have ;

array _a &original;

array _b &recoded ;

do over _a ;

if 1 <= _a <= 2 then _b = 0 ;

else if 3 <= _a <= 4 then _b=1 ;

else _b=.;

end;

run;

🔒 This topic is solved and locked.