BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
racekarrz2
Calcite | Level 5

I am trying to reverse code some variables in a 4-point likert scale. I do not want to include the missing variables, or those that said "I don't know" (98) or "Prefer not to answer" (99).

 

my code is set up like this:

 

data DISRUPT_likert1;

set DISRUPT_likert;

IF  (Q15_1 ne .) OR (Q15_1 ne 99) OR (Q15_1 ne 98) THEN Q15_1RC = 5-Q15_1;

run;

 

It is still including 99 and 98. Can someone help me find why?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Try

IF  Q15_1 not in ( 99 98) THEN Q15_1RC = 5 - Q15_1;

 

IN tests for values to be any of those in the parentheses. The list can be space or comma delimited.

 

When you combine comparisons with an OR then the result is 'True' if any of the pieces are.

So when the value is 98 then Q15_1 ne . is true.

 

You might consider naming the variable RC_Q15_1. The way SAS works you can easily make list of variables that have a common prefix but not a suffix. The may come a time when you want to do something with all of the RC variables (an array perhaps or get a Sum/Mean/Range or other calculations or Keep or Drop). Using RC_:  would build a list of all variables whose names start with RC_.

View solution in original post

3 REPLIES 3
ballardw
Super User

Try

IF  Q15_1 not in ( 99 98) THEN Q15_1RC = 5 - Q15_1;

 

IN tests for values to be any of those in the parentheses. The list can be space or comma delimited.

 

When you combine comparisons with an OR then the result is 'True' if any of the pieces are.

So when the value is 98 then Q15_1 ne . is true.

 

You might consider naming the variable RC_Q15_1. The way SAS works you can easily make list of variables that have a common prefix but not a suffix. The may come a time when you want to do something with all of the RC variables (an array perhaps or get a Sum/Mean/Range or other calculations or Keep or Drop). Using RC_:  would build a list of all variables whose names start with RC_.

racekarrz2
Calcite | Level 5
Great this worked, thank you! I also took your advice and changed all the reverse coded variables to start with "RC"
ballardw
Super User

If you have multiple variables that need the same calculations then arrays are handy. For example making up some variable names to use. If you haven't used arrays yet they are a shorthand way of doing the same thing to multiple variables (among a few other bits).

The array statement defines the array with the first thing the name (can't be an existing variable) and the list of variables that will be referenced. Note that placing the two array definitions next to each other you can easily check that the number of elements in the Q (question) and RC (Recode) lists are the same. You might even figure out how to use editor search and replace on a copy of the Array Q statement to do most of the renaming in the Array RC. The Dim function returns a count of defined elements. You use arrayname(index number) to reference the elements. So q[i] is Q15_1. You can use () or [] to enclose the index value. I tend to us [] so it is easier to tell array reference from all the other () that can appear in a complex expression.

   array q (*)    Q15_1    Q17_2    Q20_1;
   array rc(*) RC_Q15_1 RC_Q17_2 RC_Q20_1;
   do i=1 to dim(q);
      IF  q[i] not in ( .  99 98) THEN rc[i] = 5 - q[i];
   end;

Note that the index must be an integer and can be an expression which can be handy as well.

There are functions WHICHN and WHICHC that can be used to search for a value from a list. One use of arrays is to be short had for that list:  WHICHN(3, of q[*]) will return the  element number of the first variable having the value 3. WHICHC is for character values.

The functions SUM, MIN, MAX, MEAN, Call Missing and most of the functions that accept a list of variables will also use the "of array[*]" as a list of variables for the function.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 220 views
  • 0 likes
  • 2 in conversation