Solved
Contributor
Posts: 68

# How to substitute the value of a variable?

Dear guys,

I need to calculate a new variable Course which is equal to 0.2*Quiz+0.3*Midterm+0.5*Final;

Quiz is character variable, but it has numerical equivalents as follows: A = 95, B = 85, C = 75, D = 70, and F = 65.

My problem is how to substitute the value of Quiz with its numerical equivalents,instead of using " if/then" "else if" to create a new variable which represents the numerical equivalents of Quiz? Thank you.

data school;

input Age Quiz : \$1. Midterm Final;

select(Quiz);

when(A) set Quiz=95;

when(B) set Quiz=85;

when(C) set Quiz=75;                         I failed by using "select when"

when(D) set Quiz=70;

when(F) set Quiz=65;

otherwise;

end;

Course=0.2*Quiz+0.3*Midterm+0.5*Final;

datalines;

12 A 92 95

12 B 88 88

13 C 78 75

13 A 92 93

12 F 55 62

13 B 88 82

;

run;

Accepted Solutions
Solution
‎09-02-2014 10:55 PM
Posts: 1,270

## Re: How to substitute the value of a variable?

Hi,

Your select syntax needs a few modifications. New variable Quiznum has been created containing numeric values for Quiz.

data school;

input Age Quiz : \$1. Midterm Final;

select(Quiz);

when('A') Quiznum=95;

when('B') Quiznum=85;

when('C') Quiznum=75;

when('D') Quiznum=70;

when('F') Quiznum=65;

otherwise;

end;

Course=0.2*Quiznum+0.3*Midterm+0.5*Final;

datalines;

12 A 92 95

12 B 88 88

13 C 78 75

13 A 92 93

12 F 55 62

13 B 88 82

;

run;

proc print data=school;

run;

All Replies
Super Contributor
Posts: 308

## Re: How to substitute the value of a variable?

Lots of different ways to approach this. But the following is simple.

data school;

input Age Quiz : \$1. Midterm Final;

if quiz = 'A' then quiznum = 95;

else if quiz = 'B' then quiznum=85;

else if quiz= 'C' then quiznum = 75;

else if quiz = 'D' then quiznum = 70;

else quiznum=65;

Course=0.2*Quiznum+0.3*Midterm+0.5*Final;

datalines;

12 A 92 95

12 B 88 88

13 C 78 75

13 A 92 93

12 F 55 62

13 B 88 82

;

;;;;;

run;

Posts: 5,540

## Re: How to substitute the value of a variable?

If you are actually reading data from a text file, you can do the conversion to numeric marks conveniently with a user-defined informat :

/* Define informat MARKS that handles both letter and numeric marks */

proc format;

invalue marks (upcase default=4)

"A" = 95

"B" = 85

"C" = 75

"D" = 70

"F" = 65

OTHER = [4.];

run;

/* Read marks using informat MARKS, calculate course average */

data school;

input Age (Quiz Midterm Final) (:marks.);

Course = 0.2*Quiz + 0.3*Midterm + 0.5*Final;

datalines;

12 A 92 95

12 B 88 88

13 C 78 75

13 A 92 93

12 F 55 62

13 B 88 82

;

proc print data=school noobs; run;

PG

PG
Contributor
Posts: 68

## Re: How to substitute the value of a variable?

SAS Expert! Thank you!

Contributor
Posts: 68

## Re: How to substitute the value of a variable?

Hi PG,

Please forgive my ignorance, what does "default=4" and " [4.] " here mean?

I found if I delete (upcase default=4) and OTHER = [4.], I actually get the same result.

Thank you.

Posts: 5,540

## Re: How to substitute the value of a variable?

Default=4 is the default width of the informat so that marks. is the same as marks4.

OTHER=[4.] means read other values using the 4. informat

removing those is OK since OTHER values will be read with the default informat which is BEST. (I think)

PG

PG
Contributor
Posts: 68

## Re: How to substitute the value of a variable?

Great to learn from you!

Solution
‎09-02-2014 10:55 PM
Posts: 1,270

## Re: How to substitute the value of a variable?

Hi,

Your select syntax needs a few modifications. New variable Quiznum has been created containing numeric values for Quiz.

data school;

input Age Quiz : \$1. Midterm Final;

select(Quiz);

when('A') Quiznum=95;

when('B') Quiznum=85;

when('C') Quiznum=75;

when('D') Quiznum=70;

when('F') Quiznum=65;

otherwise;

end;

Course=0.2*Quiznum+0.3*Midterm+0.5*Final;

datalines;

12 A 92 95

12 B 88 88

13 C 78 75

13 A 92 93

12 F 55 62

13 B 88 82

;

run;

proc print data=school;

run;

🔒 This topic is solved and locked.