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

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
FreelanceReinh
Jade | Level 19

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

Reeza
Super User

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.

Astounding
PROC Star

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.

fengyuwuzu
Pyrite | Level 9

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.

Astounding
PROC Star

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

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 6 replies
  • 849 views
  • 2 likes
  • 4 in conversation