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

I have a 10 item questionnaire in which all 10 questions have the same answer choices: “never”, ‘almost never”, “sometime”, “fairly often” and “very often”. I want to change the values in the dataset from text characters to number characters ranging from 0 to 4. However, I do not want to write 500 if/then statements. I need a code that searches all 10 questions finds the specified “text” and changes it to the corresponding number value.

I tried to do arrays and do over statements but I don’t know how to get the code just right. How can I modify the code below to produce what I want?  Thank you.

 

array b pss_oppera_q1-pss_oppera_q10;

do over b;

if b = "Never" then b = 0;

if b = "Almost never" then b = 1;

if b = "Sometimes" then b = 2;

if b = "Fairly often" then b = 3;

if b = "Very often" then b = 4;

end;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not really on topic for the question, but you can replace the if syntax with a select for readability terms:

array b...;
array c...;
do over b;
  select(b);
    when ("Never") c=0;
    when ("Almost never") c=1;
    when ("Sometimes") c=2;
    when ("Fairly Often") c=3;
    otherwise c=4;
  end;
end;

View solution in original post

6 REPLIES 6
Astounding
PROC Star

Assuming that you are actually replacing the original variables, those variables are character.  So you need to put the values in quotes:

 

do over b;

if b = "Never" then b = "0";

else if b = "Almost never" then b = "1";

else if b = "Sometimes" then b = "2";

else if b = "Fairly often" then b = "3";

else if b = "Very often" then b = "4";

end;

jcqbano
Calcite | Level 5

I made a mistake in my original post, I didn't mean to say "number CHARACTER", I want to change these text values into number values.

Astounding
PROC Star

To save numeric values, you will need a new set of numeric variables:

 

array c num_q1 - num_q10;

 

Then inside the loop it will probably work to say:

 

do over b;

if b = "Never" then c = 0;

if b = "Almost never" then c = 1;

if b = "Sometimes" then c = 2;

if b = "Fairly often" then c = 3;

if b = "Very often" then c = 4;

end;

 

I can't test this right now, but it should work.

ballardw
Super User

Formats are another way.

proc format library=work;
invalue Response upcase
"NEVER"        = 0
"ALMOST NEVER" = 1
"SOMETIMES"    = 2
"FAIRLY OFTEN" = 3
"VERY OFTEN"   = 4
;
run;

/* in a data step*/
array b pss_oppera_q1-pss_oppera_q10;
array c num_q1 - num_q10;
do i=1 to dim(b);
   c[i]= input(b[i],Response.);
end;

If your original data was read from a text file you could use the custom informat when reading that and start with the numeric values in the data.

 

 

Note: I used the Upcase option in case your example data may vary from the actual values by case of the letters.

Reeza
Super User

Or create a new variable rather than leave it as B. 

 

Note that do over loops are deprecated but support remains for legacy. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not really on topic for the question, but you can replace the if syntax with a select for readability terms:

array b...;
array c...;
do over b;
  select(b);
    when ("Never") c=0;
    when ("Almost never") c=1;
    when ("Sometimes") c=2;
    when ("Fairly Often") c=3;
    otherwise c=4;
  end;
end;

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
  • 6 replies
  • 10203 views
  • 6 likes
  • 5 in conversation