BookmarkSubscribeRSS Feed
johnlennon
Calcite | Level 5

Hello,

I have this question " Create a new variable called project2 and add 10 points to the project grade of the following students A,B,C,D (keep the grades of other students same)" 

Can you please help me out ?

6 REPLIES 6
SASKiwi
PROC Star

Something similar to this:

 

if student in ('A', 'B', 'C') then grade = grade + 10;

 

BTW, it would be helpful if you summarised your problem in the title rather than ask for help because that applies to most posters...Smiley Happy

 

Perhaps something like: SAS Coding Question

 

I partially agree with SASKiwi's answer except that the grade variable will be over written and you asked for a new variable. Therefore I suggest:

 

if student in ('A', 'B', 'C') then project2 = project + 10;
MikeZdeb
Rhodochrosite | Level 12

Hi, another idea ...

 

data x;
input student :$1. grade @@;
datalines;
A 50 B 60 C 70 D 33
;

data y;
set x;
grade + 10*(student in ('A' 'B' 'C'));
run;

 

data set Y ...

Obs student grade

1     A      60
2     B      70
3     C      80
4     D      33

 

hbi
Quartz | Level 8 hbi
Quartz | Level 8

Here is a slight variation of Mike's example. This one handles missing values. 

 

data x;
    input student $ grade;
    datalines;
    A 50
    B 60
    C .
    D 33
;
run;

data y;
    set x;
    if student in ('A','B','C','D') then project2 = sum(grade, 10);
run;

 

MikeZdeb
Rhodochrosite | Level 12

Hi, the SUM statement also ignores mising values.

 

data x;
input student :$1. grade @@;
datalines;
A 50 B 60 C . D 33
;


data y;
set x;
grade + 10*(student in ('A' 'B' 'C'));
run;

 

data set Y ..

Obs student grade

1    A      60
2    B      70
3    C      10
4    D      33

 

hbi
Quartz | Level 8 hbi
Quartz | Level 8

You are right ... the boolean statement student in ('A' 'B' 'C') evaluates to 1; thus, for student 'C', 1*10 = 10. 

 

In short, the sum operator (+) used in conjunction with the product of a boolean result would result in a non-missing value. 

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
  • 1029 views
  • 0 likes
  • 5 in conversation