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

Hello Everyone

 

First, please take a look at this data set:

 

ID     College1     College2     NewVariable
1            A                  A                YES
1            A                  B                YES
1            A                  A                YES
2            A                  B                 NO
2            A                  B                 NO
2            A                  B                 NO
3            A                  A               YES
3            A                  A               YES
3            A                  A               YES

 

My goal is to create NewVariable. Basically, if none of the values of College2 = College1, then NewVariable = No. If all the values of College2 = College1, or if some of the values of College2 = College1, then NewVariable = Yes.

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
proc sql;
	create table want as select *,sum(college1=college2) as num
		from have group by id;
quit;

basically, this creates a new variable NUM which counts the number of matches. I will leave it up to you to turn the 0 match case into "NO" and the cases where there is 1 or more matches into "YES".

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
proc sql;
	create table want as select *,sum(college1=college2) as num
		from have group by id;
quit;

basically, this creates a new variable NUM which counts the number of matches. I will leave it up to you to turn the 0 match case into "NO" and the cases where there is 1 or more matches into "YES".

--
Paige Miller
bourdeax
Fluorite | Level 6

Thank you, Paige. That worked. Also, I appreciate how you answered my main question but left some work for me to do. That is a good teaching technique.

bourdeax
Fluorite | Level 6

Also, in case anyone wants to know what I did after Paige's section of code:

 

data want;
     set have;
     if num = 0 then flag = "N";
         else flag = "Y";
run;

Kurt_Bremser
Super User

Data step solution:

data want;
set have;
by id;
retain newvar;
if first.id then newvar = 'N';
if college1 = college2 then newvar = 'Y';
if last.id;
keep id newvar;
run;

If needed, you can join that back to the original dataset.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1200 views
  • 3 likes
  • 3 in conversation