Reputation: 21
I want to assign a new variable from existing highest n variable. So if we have a table that has increasing number of columns -
data have;
input uid $ var1 $ var2 $ var3 $;
datalines;
1111 1 0 1
2222 1 0 0
3333 0 0 0
4444 1 1 1
5555 0 0 0
6666 1 1 1
;
I want derive the variable var3 as final_code.
data want;
set have;
final_code = max(of var1-var3);
run;
Above doesn't make sense here as I want only var3 column to remain. Similarly, if var4 is there, I wish to have var4 only.
Does somebody want to help me here ?
Upvotes: 0
Views: 41
Reputation: 63424
If I understand you right, you don't want max
of the values but the value from the highest-numbered-variable.
Lots of ways to do this, which way depends on how the variables are named. Here's the easiest, if they're actually named as you say.
data want;
set have;
array var[*] var:;
final_code = var[dim(var)];
run;
Here we make an array out of var:
and then choose the last element in the array using dim
(to say the size of the array).
Upvotes: 3
Reputation: 12465
I think this is what you are looking for is:
%let n=3
data want;
set have;
var&n = max(of var1-var&n);
drop var1-var%eval(&n-1);
run;
The macro variable &n
holds the value of n. This acts as a substitution during the compilation phase of the code.
The DROP
statement tells the data step to drop those variable.
The %eval()
macro function performs integer math on macro values. So we are dropping 1 through N-1.
Upvotes: 0