0

I have a dataset in SAS with

  • a variable condition, that can take values in "01",...,"20"
  • several variables indexed by i, for instance var01, ..., var20

What i want to do is to create a new variable total which is equal to vark if condition=k. I can do it by several if... else statement (it is what I do for now), but I did not manage to do it in a more compact and elegant way.

I tried

data want;
set have;
    call symput(condition, temp);
    total=var&temp;
run;

But it does not work... At best with some tries with resolve instructions I wan get a total value that equals 'var01' and so on, but just the characters, not the values associated to the variable var01

My goal is to do better than

data want  ;
set have (keep=noi nomen var01-var20 lprm condition ag);
if condition="01" then varpr=var01;
else if condition="02" then varpr=var02;
else if condition="03" then varpr=var03;
else if condition="04" then varpr=var04;
else if condition="05" then varpr=var05;
else if condition="06" then varpr=var06;
else if condition="07" then varpr=var07;
else if condition="08" then varpr=var08;
else if condition="09" then varpr=var09;
else if condition="10" then varpr=var10;
else if condition="11" then varpr=var11;
else if condition="12" then varpr=var12;
else if condition="13" then varpr=var13;
else if condition="14" then varpr=var14;
else if condition="15" then varpr=var15;
else if condition="16" then varpr=var16;
else if condition="17" then varpr=var17;
else if condition="18" then varpr=var18;
else if condition="19" then varpr=var19;
else if condition="20" then varpr=var20;
run;

Thanks

Anthony Martin
  • 767
  • 1
  • 9
  • 28
  • 1
    The key thing to understand here is that SAS resolves `&temp` before the data step is even run. – DWal Mar 06 '15 at 16:08

3 Answers3

3

A different solution, not using arrays, using the vvaluex function:

varpr = vvaluex(catt('var', condition));

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002233818.htm

Reeza
  • 20,510
  • 4
  • 21
  • 38
1

You can't use a macro variable that way - it has to be defined outside of the data step.

However, you can use arrays for this, if I understand your purpose.

data want;
  set have;
  array var[20] var01-var20;
  total=var[condition];
run;
Joe
  • 62,789
  • 6
  • 49
  • 67
  • My purpose is to select the value of the correct var_i variable in a new variable, the correct var_i being the one with the i which is in 'condition'. For instance, for one observation for which condition="02", i want to have total=var02 (the value of it, var02 is a variable I have along with var01... and so on). – Anthony Martin Mar 06 '15 at 15:42
  • Right. That's what this does. (You would need to make sure condition was a numeric, if it's character then `input` it.) – Joe Mar 06 '15 at 15:43
  • I have tried `condition_2=input(condition,2.)` (which is indeed a character), but it does not work – Anthony Martin Mar 06 '15 at 16:01
  • 1
    "It does not work" isn't specific enough to help. Your variables have leading zeros in front of them, so to put them in an array, you'd add `var01-var20` to the end of the `array` statement. You could also just put the `input` function right in the array subscript like this: `total=var[input(condition,2.)];`. – DWal Mar 06 '15 at 16:25
  • Edited to reflect the variable names being 0 padded. – Joe Mar 06 '15 at 16:28
0

Here you can create a small macro with loop.

Idea is

%Macro cond;

%do i=1 %to 20;

If condition="&i" then out_var=in_&var;

%end;

%mend;

Use this macro in data step.

I hope this can solve your purpose.