简体   繁体   中英

SAS: passing varlist to sas macro

Except for SUM, SAS really lacks row functions. I wanted to count certain extended missing codes within waves of a longitudinal dataset.

I can use arrays to process over a hard coded varlist for each wave, but I had no luck making a macro that I could call for each wave. The problem seemed to be no way to pass in the varlist, especially if using first--last notation.

data xxx;
   input a b c d e f;
   datalines;
 1  2  3  4  5  6
.w .w .w .w .w .w
 3  4  5 .w .w .w
    ;
 run;

data yyy(drop=i); set xxx;    
  array wave1vars(*) a--c;
  wave1count = 0;
  do i = 1 to dim(wave1vars);
     if wave1vars(i) = .w then wave1count = wave1count +1;
  end;

  array wave2vars(*) d--f;
  wave2count = 0;
  do i = 1 to dim(wave2vars);
     if wave2vars(i) = .w then wave2count = wave2count +1;
  end;
run;

Why do you need a macro? Why not just use a multi-dimensional array. Unless the number of questions is different in each wave?

missing w;
data have;
  input a b c d e f;
cards;
 1  2  3  4  5  6
 w  w  w  w  w  w
 3  4  5  w  w  w
;;;;

data want ;
  set have ;
  array wavevars (2,3) a--c d--f ;
  array wavecount (2) ;
  do i=1 to dim(wavecount);
    wavecount(i)=0;
    do j=1 to dim2(wavevars);
      wavecount(i)+.w = wavevars(i,j);
    end;
  end;
  drop i j ;
run;

You can also avoid arrays altogether, by using the CATT() function to convert the values to a concatenated character string, then counting the number of W's found in the string, like:

343  data want;
344    input a b c d e f;
345    wave1count=countc(catt(of a--c),'W');
346    wave2count=countc(catt(of d--f),'W');
347    put _all_;
348  datalines;

a=1 b=2 c=3 d=4 e=5 f=6 wave1count=0 wave2count=0 _ERROR_=0 _N_=1
a=W b=W c=W d=W e=W f=W wave1count=3 wave2count=3 _ERROR_=0 _N_=2
a=3 b=4 c=5 d=W e=W f=W wave1count=0 wave2count=3 _ERROR_=0 _N_=3
NOTE: The data set WORK.WANT has 3 observations and 8 variables.

352  ;
353  run;

Try

wave1count = nmiss(of a--c);
wave2count = nmiss(of d--f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM