BookmarkSubscribeRSS Feed
elsalam
Calcite | Level 5

I tried to assign a new value to match with another value by group.

 

If I use the code below, it came out all "AFC-WEST'.

 

Conference = propcase(Team);

Retain Conference;

if Team = 'Ravens to Viking then Conference = 'AFC-NORTH';

else Conference = 'AFC-WEST;

 

The result should look at below.

 

TeamConference
CampbellAFC-North
NorthRaleighAFC-North
BatterAFC-North
CreekAFC-North
SouthAFC-West
MiamiAFC-West
BellfortAFC-West
VikingAFC-South
ChiefAFC-South
7 REPLIES 7
novinosrin
Tourmaline | Level 20

what do you mean by the if statement

 

 

Conference = propcase(Team);

Retain Conference;

if Team = 'Ravens to Viking then Conference = 'AFC-NORTH';?/*for one, it is syntactically incorrect and 2. are you trying a range from Ravens--Viking*/

else Conference = 'AFC-WEST;

elsalam
Calcite | Level 5
TeamConference
CampbellAFC-North
NorthRaleighAFC-North
BatterAFC-North
CreekAFC-North
SouthAFC-West
MiamiAFC-West
BellfortAFC-West
VikingAFC-South
ChiefAFC-South

 

If team = 'Batter tp Creek' then Conference = 'AFC-NORTH';

I was trying to do the matching.  Batter and Creek are the value at Team column so it is Batter and CreeK.  SAS will assign AFC-NORTH to these value. 

 

I really have no idea how to match.

ballardw
Super User

When you have many character values to compare one way is to use the IN operator but you need to list every value you are looking for:

 

if team in ('Ravens' 'Otherteamname' 'Vikings') then ...

novinosrin
Tourmaline | Level 20

aah ok, 

are you trying to do this?

 

Conference = propcase(Team);

Retain Conference;

if Team =in ('Ravens' ,' Viking') then Conference = 'AFC-NORTH';

else Conference = 'AFC-WEST;

 

elsalam
Calcite | Level 5

Yes, I changed it to

if team in ('Campbell','Batter') then Conference = 'AFC-NORTH';

else Conference = 'AFC-WEST';

 

It still the entire column are AFC-WEST.

 

And, if I have more than two value to be matched.

 

Is it a faster way to match them?

novinosrin
Tourmaline | Level 20

Try proc format, load your look up values in proc format and apply in a datastep

easy to maintain and edit

ballardw
Super User

@elsalam wrote:

Yes, I changed it to

if team in ('Campbell','Batter') then Conference = 'AFC-NORTH';

else Conference = 'AFC-WEST';

 

It still the entire column are AFC-WEST.

 

And, if I have more than two value to be matched.

 

Is it a faster way to match them?


Show the entire data step and any messages from log. Paste into a code box opened using the forum {I} icon to preserver formatting of code and any error or warning messages. It might also help to provide some actual values from you data as a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Note that the comparison matches the entire field. So if the value has any leading blanks or other characters than "Campbell" the match will return false or if the data is not in the same case, "campbell" is not the same as "CAMPBELL" or "Campbell" or "cAmpbell"

 

The list of values used in the in comparison can be more than two, I just didn't feel like typing more.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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