LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (May 2008, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Fri, 16 May 2008 16:28:41 -0400
Reply-To:   "Howard Schreier <hs AT dc-sug DOT org>" <schreier.junk.mail@GMAIL.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   "Howard Schreier <hs AT dc-sug DOT org>" <schreier.junk.mail@GMAIL.COM>
Subject:   Re: Add certain variables across a dataset

On Fri, 16 May 2008 12:35:05 -0700, shankera <shanker_anita@HOTMAIL.COM> wrote:

>On May 16, 1:56 pm, muthia.kachira...@GMAIL.COM (Muthia Kachirayan) >wrote: >> On Fri, May 16, 2008 at 11:50 AM, shankera <shanker_an...@hotmail.com> >> wrote: >> >> > I have a dataset with 100s of variables, and I want to add ones with >> > the suffix "_pending". Is there a way to do it without having to >> > write out 30 some variable names? >> >> Using the vname function of the variable and whether it has >> underscore-character in the name, the summation can be done for all >> _numeric_ variables in the data set. Instead of underscore, the word, >> pending, can be looked as an alternative. >> >> data new; >> input f_pending g_pending l_pending h_pending control; >> cards; >> 5 56 5 2 7 >> 6 2 5 6 52 >> 5 5 4 5 78 >> 6 5 2 8 9 >> 7 5 2 1 7 >> ; >> run; >> data need; >> length name $32; >> set new; >> array nv _numeric_; >> do over nv; >> name = vname(nv[_i_]); >> if indexc(name,'_') then sum = sum(sum, nv[_i_]); >> * if index(name,'pending') then sum = sum(sum, nv[_i_]); >> end; >> run; >> proc print data = need; >> run; >> >> Muthia Kachirayan > > > >Thanks! I figured out using similar code.

It's only that complicated because the data structure is rather suboptimal. Suppose that instead the data were in a normalized table such as the one generated by:

data new; id + 1; length item $ 8; do item = 'f','g','l','h','control'; input value @; pending = item in ('f','g','l','h'); output; end; cards; 5 56 5 2 7 6 2 5 6 52 5 5 4 5 78 6 5 2 8 9 7 5 2 1 7 ;

Then getting the sums of the pending items would be as easy as:

proc summary data=new nway; where pending; class id; output out=need sum(value)=; run;


Back to: Top of message | Previous page | Main SAS-L page