1

In SAS, I have a few columns of dollar values and several other columns. I don't care about a row of data if it has 0 values for all dollar values and I don't want these rows in the final data. The columns that are dollar values are already in an array Fix_Missing. I am in the data step.

So, for example, I could do:

IF Fix_Missing{1} NE 0 OR Fix_Missing{2} NE 0 OR Fix_Missing{3} NE 0;

However, the number of variables in the array may change and I want code that works no matter how many variables are in it. Is there an easy way to do this?

GeoffDS
  • 1,221
  • 5
  • 19
  • 31

1 Answers1

3

In most functions that accept uncounted lists of variables, you can use of array[*].

if sum(of fix_missing[*]) > 0

for example would work assuming fix_missing cannot have negative values. You can also use this with CATS, so for example:

if cats(of fix_missing[*]) ne '000';

would work, and you could do something even like this:

if cats(of fix_missing[*]) ne repeat('0',dim(fix_missing)-1);

if you might have unknown numbers of array elements (repeat takes a character and repeats it n+1 times).

Another useful function, probably not useful here but in the same vein, is whichn and whichc (numeric/character versions of same thing). If you wanted to know if ANY of them had a 0 in them:

if whichn(0,of fix_missing[*]) > 0;

which returns the position of the first zero it finds.

Joe
  • 62,789
  • 6
  • 49
  • 67
  • So, a 0 number will always be turned into '0' as a string and not '0.00' or something of that sort? – GeoffDS Dec 09 '14 at 20:47
  • 1
    0 will become `" 0"` by default (which displays badly in comments, thanks markdown, but is eleven spaces plus a zero at the end); `cats` will remove those spaces. The default number format in SAS is `best12.` in most contexts, which means place the decimal wherever it needs to best display the number - integer if it's an integer, with decimal if decimal, and follow some rules to deal with digits beyond 12 total length. – Joe Dec 09 '14 at 20:52