DATA Step, Macro, Functions and more

Creating a Series of Indicator Variables Based on a Set of Criteria

Reply
Contributor
Posts: 22

Creating a Series of Indicator Variables Based on a Set of Criteria

Hello,

I would like to create a long series of indicator variables based on a set of criteria. The code I am using looks like this:

data test;
     set sample;
     array ind[91];
     do i=1 to dim(ind);
     do test=1 to 10 by 0.1;
          ind = (tenure >= test and risk="low" and value="low");
     end;
     end;
run;

The basic idea is to create ind1 to ind99 based on whether tenure is greater than or equal to the test counter and whether the levels of risk and value are low. Running this, I seem to be getting 0s for the indicator variables created, even when the values for a particular record meet the criteria.

As a secondary matter, I would like to ideally generalize this because I two other value levels for risk (medium and high) and 4 other value levels for value. My approach now is to create other sets of array variables for the other combinations of risk and value. But I suspect there is a better way. Would a multidimensional array help here?

Thanks,

Dhrumil Patel

Super User
Posts: 19,855

Re: Creating a Series of Indicator Variables Based on a Set of Criteria

Posted in reply to dhrumil_patel

Why do you need all these indicator variables?

At any rate, why loop through it all? Set the array default to  and only start setting to 1 when tenure=test for starters.

Second, you can use do loops to loop over character values.

do value="low", "medium", "high";

output;

end;

Super User
Posts: 11,343

Re: Creating a Series of Indicator Variables Based on a Set of Criteria

Posted in reply to dhrumil_patel

As written your loop

do test=1 to 10 by 0.1;

          ind = (tenure >= test and risk="low" and value="low");     

end;

 

1) does not properly reference the array, to refernce an element of the array you want something like: ind[i] = (<code goes here>);

2) is only ever going to yield the result for test= 10

Ask a Question
Discussion stats
  • 2 replies
  • 219 views
  • 0 likes
  • 3 in conversation