DATA Step, Macro, Functions and more

recoding variable that has different values but they are all the same

Accepted Solution Solved
Reply
Super Contributor
Posts: 312
Accepted Solution

recoding variable that has different values but they are all the same

my variable has different values, like:

ach

ach (optioinal)

bank transfer (ach)

bank_transfer

online banking transf

onlinebanking

online transfer

 

They all mean the same thing.

 

I want to use if ... then ... to recode them, can I use "contain" or "like" or "in" operators in the if statement?

 


Accepted Solutions
Solution
‎04-08-2016 01:49 PM
Super User
Posts: 5,096

Re: recoding variable that has different values but they are all the same

[ Edited ]

You should consider creating a permanent format (or a permanent informat as an alternative).  Here's what a temporary format might look like:

 

proc format;

value $ach

'ach',

'ach (optioinal)',

'bank transfer (ach)',

'bank_transfer',

'online banking transf',

'onlinebanking',

'online transfer'

= 'ach';

run;

 

Then when you want to check against the list, you can use:

 

if  /* or WHERE, either will work */  put(var, $ach.) = 'ach';

 

The advantage here is that you only need to store a format once.  But you can then use the IF or WHERE statement in many programs.  If you ever need to add to the list, you don't have to change all the programs.  Update the permanently saved format and all the programs that refer to it are automatically updated.

 

Note that saving a format permanently and reusing it later is not an introductory topic.  It's not terribly long or complex, but it's not introductory.  So choose the right time to learn how to do those things.

 

Good luck.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,115

Re: recoding variable that has different values but they are all the same

The IN operator can be used in IF statements, but CONTAINS and LIKE cannot. Use character functions instead.

Super User
Posts: 17,936

Re: recoding variable that has different values but they are all the same

Instead of contains use FIND() or INDEX()

Instead of like you can use : to do match from the start, but I would recommend perhaps, compged or some of those similarity functions.

Solution
‎04-08-2016 01:49 PM
Super User
Posts: 5,096

Re: recoding variable that has different values but they are all the same

[ Edited ]

You should consider creating a permanent format (or a permanent informat as an alternative).  Here's what a temporary format might look like:

 

proc format;

value $ach

'ach',

'ach (optioinal)',

'bank transfer (ach)',

'bank_transfer',

'online banking transf',

'onlinebanking',

'online transfer'

= 'ach';

run;

 

Then when you want to check against the list, you can use:

 

if  /* or WHERE, either will work */  put(var, $ach.) = 'ach';

 

The advantage here is that you only need to store a format once.  But you can then use the IF or WHERE statement in many programs.  If you ever need to add to the list, you don't have to change all the programs.  Update the permanently saved format and all the programs that refer to it are automatically updated.

 

Note that saving a format permanently and reusing it later is not an introductory topic.  It's not terribly long or complex, but it's not introductory.  So choose the right time to learn how to do those things.

 

Good luck.

Super Contributor
Posts: 312

Re: recoding variable that has different values but they are all the same

thank you very much. Would you mind telling me how to save the format permenantly?

 

I do have this problem, that when I tried to open a data set for which I used a self defined format, SAS complains about the format and I have to find the code containing the format and run the format again, to open the data file.

Super User
Posts: 5,096

Re: recoding variable that has different values but they are all the same

To save a format permanently, you need to add the LIBRARY= option to the PROC FORMAT statement.  To retrieve permanently saved formats, you need to add the FMTSEARCH global option to your program.  You can look those up in the documentation, or you can find some decent examples in this paper:

 

http://www.ats.ucla.edu/stat/sas/library/formats.htm

 

 

Super Contributor
Posts: 312

Re: recoding variable that has different values but they are all the same

great! thank you.

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 298 views
  • 2 likes
  • 4 in conversation