SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
tinghlin
Fluorite | Level 6

data temp1;
input teacher $ class $ score;
cards;
A C1 70
A C2 80
B C3 90
B C4 99
C C5 76
D C6 95
;
run;

data temp2;
input teacher $ class $ weight ;
cards;
A C2 0.5
B C2 0.5
B C3 0.333
C C3 0.333
D C3 0.333
;
run;

from temp1,Class C1, C4, C5 and C6 are taught independently by teacher A , B, C and respectively.

from temp2, Class C2 is co-taught by teacher A and B (weight 0.5 each), but data temp1 only has A's data not B's.

Class C3 is co-taught by teacher B, C, and D (weight 0.3333 each), again but data temp1 only has B's data not C D.

I wan to create a new data set to copy the co-teach (class and score) information as follows: variables are teacher class score and weight(only co-taught courses have weights)


A C1 70 .
A C2 80 0.5
B C2 80 0.5
B C3 90 0.333
C C3 90 0.333
D C3 90 0.333
B C4 99 .
C C5 76 . 
D C6 95 .

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Try this

 

data want(drop=s);
   merge temp1 temp2;
   by class teacher;
   if score then s = score;
   retain s;
   if score = . then score = s;
run;

 

Result

 

teacher class score weight 
A       C1    70    . 
A       C2    80    0.500 
B       C2    80    0.500 
B       C3    90    0.333 
C       C3    90    0.333 
D       C3    90    0.333 
B       C4    99    . 
C       C5    76    . 
D       C6    95    . 
PeterClemmensen
Tourmaline | Level 20

Simpler:

 

data want;
   merge temp1 temp2;
   by class teacher;
   if score then _iorc_ = score;
   else score = _iorc_;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 568 views
  • 1 like
  • 2 in conversation