Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a SAS macro run through variables/columns?

Tags:

sas

sas-macro

I am trying my hand at SAS macros for the first time. My basic question is: I have a dataset with some 10000 variables. I need to take each column individually, create a new conditional variable, store the results, then move to the next column. These columns are not sequentially ordered. What is the syntax SAS uses to identify a column (similar to how "_N_" would identify a row)?

Here is more information. The data looks something like this:

ID  v1   v2   v3  ... v10000
01  3.2  1.5  7.8 ...   4.2
02  1.1  4.5  1.9 ...  10.7
..
 N  2.5  1.5  4.9 ...   7.3

I need to look at the values of v1, count how many obs are above a value x and how many are below a value x, record those numbers in a dataset, then move onto v2, v3, ... v10000. In the end, I'd have a dataset that would show the number of obs above value x and number of obs below value x for each of my 10000 variables.

I have written the code as I would have written it for one variable in standard SAS code and it works, and now my intention is to convert that code into macro code, but I do not know how to construct a loop that would move from one column to the next.

Any help or references you could give would be greatly appreciated.

Thanks.

like image 561
Samantha Avatar asked Nov 21 '25 14:11

Samantha


1 Answers

%LET CUTOFF = 3.1415926 ; /* set this as your 'cutoff' value */

data counters ;
  set mydata end=eof ;

  array vi{*} v1-v10000 ; /* incoming values */
  array vc{*} c1-c10000 ; /* counters */

  retain vc . ;

  do i = 1 to dim(vi) ;
    if vi{i} >= &CUTOFF then vc{i} + 1 ;
  end ;

  if eof then output ;

  keep c1-c10000 ;
run ;
like image 65
Chris J Avatar answered Nov 24 '25 19:11

Chris J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!