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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1030 views
  • 4 likes
  • 3 in conversation