BookmarkSubscribeRSS Feed
dhrumil_patel
Fluorite | Level 6

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

2 REPLIES 2
Reeza
Super User

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;

ballardw
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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