DATA Step, Macro, Functions and more

assign new value to other value

Reply
Contributor
Posts: 20

assign new value to other value

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
PROC Star
Posts: 1,340

Re: assign new value to other value

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;

Contributor
Posts: 20

Re: assign new value to other value

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.

Super User
Posts: 13,066

Re: assign new value to other value

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 ...

PROC Star
Posts: 1,340

Re: assign new value to other value

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;

 

Contributor
Posts: 20

Re: assign new value to other value

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?

PROC Star
Posts: 1,340

Re: assign new value to other value

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

easy to maintain and edit

Super User
Posts: 13,066

Re: assign new value to other value


@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.

Ask a Question
Discussion stats
  • 7 replies
  • 100 views
  • 4 likes
  • 3 in conversation