Hello,
Make it simple.
Let's say that I want to define one variable (ex: var4) based on three variables (ex: var1, var2, and var3), considering that based on the various combinaison of var1 var2 and var3, var4 could take many different values.
The first approach that I have in mind will be the if and condition ex:
if var1 eq something and var2 eq something else and var3 eq something then var4=specific_value
and so on.
There is also the proc format but I dont know how to define / map a new variable based on three variables.
Which approach is the more efficient
I think you need to provide a better description (an example) of what you're trying to do.
As you say, you can use PROC FORMAT to map one value to another.
Once I had a problem where I needed to map three values into another value. I started to build a new variable that would have the three values concatenated, and was going to build a format to map the concatenated value to my target value.
Then I remembered that SAS has hash tables. You can define a hash table that has 3 key values, and use that as a lookup.
As to whether a hash table is "best" or "more efficient", it really depends on your use case. There are lots of ways to do a lookup in SAS.
If you want to do equality tests then a hash object is probably the simplest, as long as the number of possible values is small. Create a "truth" or "lookup" table with all of the possible values.
So if VAR1 to VAR3 could each have only two possible values then your LOOKUP table will have 8 observations. So something like:
data lookup;
input var1 :$1. var2 var3 var4 :$8.;
cards;
A 1 1 1
A 1 1 2
A 2 1 3
A 2 2 4
B 1 1 5
B 1 2 6
B 2 1 7
B 2 2 8
;
Then you can load that into a HASH and use that to add VAR4 to your existing dataset (let's call it HAVE) and create a new dataset (let's call it WANT).
data want;
set have lookup(keep=var4 obs=0);
if _n_=1 then do;
declare hash h(dataset:'lookup');
h.definekey('var1','var2','var3');
h.definedata('var4');
h.definedone();
end;
if h.find() then call missing(var4);
run;
Can you please explain a litle bit more the hash approac and how does it works ?
I strongly encourage you to read Using the Hash Object to Store and Retrieve Data and come back with any further questions you have that need clarification.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.