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

Thank you so much!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I don't understand what your current code is doing. Why are you opening a dataset using macro code?

Why are you generating multiple datasets?

 

If you want to find observations where the last three characters are in a list of values just use:

where substrn(product_id,length(product_id)-2) in ('889' '123')

View solution in original post

10 REPLIES 10
Tom
Super User Tom
Super User

The SAS macro processor is a way to allow you to generate CODE.

So what SAS code do you want to run?

 

What type of variable is PRODUCT_ID?

If it is a character variable then something like:

where product_id like '%889'

should work.

If it is numeric then you want to do something like:

where mod(product_id,1000) = 889

What output do you want? What is the overall code you are trying to create?

Why did you mention macro? Is there some part of the code that needs to change so that you want to use a macro or perhaps just a macro variable to generate that part of the code?  How does it change? What are the types of values that you will need to support?  How do the different values change how the SAS code you need to run looks?

nancy_mancilla
Calcite | Level 5

Thanks!

nancy_mancilla
Calcite | Level 5
Value to look up
8885

Value expected
1274885
8841885
Reeza
Super User

Is that a typo or is that what you expect to match? 

 


@nancy_mancilla wrote:
Value to look up
8885

Value expected
1274885
8841885

 

nancy_mancilla
Calcite | Level 5

thanks!

Reeza
Super User
I assure you using IN with the colon as I've shown does work so you're doing it wrong - or trying to do it in SQL, which you've never stated as a requirement. Did you try my example? Or Tom's? I believe Tom's is better and works for either SQL or DATA STEP but has a similar issue where you need to ensure the variable is at least the specified number of characters.
Tom
Super User Tom
Super User

I don't understand what your current code is doing. Why are you opening a dataset using macro code?

Why are you generating multiple datasets?

 

If you want to find observations where the last three characters are in a list of values just use:

where substrn(product_id,length(product_id)-2) in ('889' '123')
nancy_mancilla
Calcite | Level 5

Thank you so much!

Reeza
Super User
Macros are not faster - they're faster for the programmer but typically operate slower as you have more code to generate.
A macro is not a good solution here.
Reeza
Super User
Use the REVERSE and WHERE with colon operator?
REVERSE flips your variable around so you're comparing the start not the end. This allows you to use the colon operator which will truncate strings to check if they match. Note that if the string can be shorter this may not work as desired - test it with two digit product_ID for example.


data want;
set have;
where reverse(product_ID) in: ('988', '211');
run;

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
  • 10 replies
  • 1131 views
  • 2 likes
  • 3 in conversation