Reputation: 186
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;
Upvotes: 1
Views: 144
Reputation: 6378
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;
Upvotes: 1
Reputation: 51566
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;
Upvotes: 1