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 (September 2011, 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 Sep 2011 13:51:49 -0500
Reply-To:     "Data _null_;" <iebupdte@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "Data _null_;" <iebupdte@GMAIL.COM>
Subject:      Re: using a hash table RE: [SAS-L] No one here to help me in
              coding
Comments: To: bbser 2009 <bbser2009@gmail.com>
In-Reply-To:  <001501cc749f$3bdeeff0$b39ccfd0$@com>
Content-Type: text/plain; charset=UTF-8

This is how I do it. This example program only creates the HASH and has no code to use it.

options _last_ = sashelp.class; data something; * arrays of ALL variables; if 0 then set _LAST_; array _c[*] _character_; array _n[*] _numeric_;

* hash look up to array index; if _n_ eq 1 then do; declare hash vr(); vr.definekey('vName'); vr.definedata('vName','vType','_i_'); vr.definedone(); vType = 'C'; do _i_ = 1 to dim(_c); vName = vName(_c[_i_]); vr.add(); end; vType = 'N'; do _i_ = 1 to dim(_n); vName = vName(_n[_i_]); vr.add(); end; vr.output(dataset:'vr'); end;

*** READ DATA and do something using VNAME to lookup array reference; /* do while(not eof);*/ /* set _last_ end=eof;*/ /* more code here */ /* output;*/ /* end;*/ stop; run; proc print data=vr; run;

On Fri, Sep 16, 2011 at 1:34 PM, bbser 2009 <bbser2009@gmail.com> wrote: > Hi Data _null_, > > I am interested in the concept of using hash tables you mentioned before. > Below is my code to implement your idea. I would appreciate if you or anyone else could make comments about it. > > BTW, I am using the 2nd and 3rd steps to avoid hard-coding the hash table in the 4th step. > > Max > (Maaxx) > > ====================== > > data have; > input v1$ v2$ x y z; > cards; > x y 1 2 3 > z x 3 4 5 > ; > > /* > proc transpose data=have(obs=0) out=variables >     name=vname; > run; > > data variables; >     set variables; >     index+1; > run; > > data have; >     if _n_=1 then >         do; >             if 0 then set variables; >             declare hash max(dataset: "work.variables"); >             max.definekey("vname"); >             max.definedata("index"); >             max.definedone(); >         end; >     set have; >     array w{*} x--z; >     max.find(key:v1); >     i=index; >     max.find(key:v2); >     j=index; >     sub=w{i}-w{j}; >     drop vname index i j; > run; > > proc print data=have; > run; > > -----Original Message----- > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Data _null_; > Sent: September-16-11 12:35 PM > To: SAS-L@LISTSERV.UGA.EDU > Subject: Re: [SAS-L] No one here to help me in coding > > You could create a list of var names of the variables in the array and > use WHICHC.  I expect it would be slightly faster than searching. > > A method I'm liking these days is to create a hash table as a look up > (by name) for the index to array(s).  Its a bit fiddly but once you > get it coded it is pretty easy to use. > > On Fri, Sep 16, 2011 at 11:14 AM, Tom Abernathy <tom.abernathy@gmail.com> wrote: >> You could brute force it by looping over array and using VNAME to find the >> index for the variable name that matches. >> (untested code) >> >> i1=0; >> do i=1 to dim(ww) while (i1=0); >> � if var1 = vname(ww(i)) then i1=i; >> end; >> >> >> >> On Fri, 16 Sep 2011 11:43:52 -0400, bbser 2009 <bbser2009@GMAIL.COM> wrote: >> >>>Tom, what if those WWs becoming u,v, w, x, y? >>>Is there a neat way to do this without using vvaluex()? >>>Thanks. >>> >>>Regards, Max >>>(Maaxx) >>> >>>-----Original Message----- >>>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Tom >>>Abernathy >>>Sent: September-16-11 9:25 AM >>>To: SAS-L@LISTSERV.UGA.EDU >>>Subject: Re: [SAS-L] No one here to help me in coding >>> >>>Vishali - >>> >>> � It looks like your problem is that you want to use the value of variable >>>as a reference to the another variable in the same data step. This is a >>>little hard to do in SAS. � I can see two methods. >>> >>> � One you could use the VVALUEX function to return the value of the >>>variable. � So if VAR1='WW1' you could use VVALUEX(var1) to return the value >>>of WW1. The problem with this is that it returns the formatted value as a >>>CHARACTER string. � This might case lose of precision, notes to the log or >>>other issues. >>> � s= vvaluex(var1) - vvaluex(var2) ; >>> >>> � If you can insure that your variables are named with nice numeric >> suffixes >>>as in your example then you could parse out the suffix and use it as the >>>index into an array. >>> >>>data have ; >>> input var1 $ var2 $ ww1-ww5 ; >>>cards; >>>WW1 WW2 1 3 1 2 4 >>>WW2 WW1 3 3 4 3 4 >>>WW3 WW4 3 2 3 3 4 >>>WW4 WW3 4 2 4 3 5 >>>WW5 WW2 4 2 2 4 5 >>>run; >>> >>>data want ; >>> � set have ; >>> � array ww ww1-ww5 ; >>> � i1 = input(substr(var1,3),best.); >>> � i2 = input(substr(var2,3),best.); >>> � s = ww(i1) - ww(i2) ; >>>run; >>> >>> � PS: If you could switch to using plain text for posting your email will >> be >>>easier to read via the SAS-L page at University of Georgia. >>> >>>On Fri, 16 Sep 2011 17:37:16 +0530, Vishali Rajiv <vishalirajiv@YAHOO.COM> >>>wrote: >>> >>>> >>> >>> >>>From: Vishali Rajiv <vishalirajiv@YAHOO.COM> >>>To: SAS-L@LISTSERV.UGA.EDU >>>Sent: Friday, 16 September 2011 10:54 AM >>>Subject: substraction code >>> >>>can you help me wity the code for substraction ,I wanted to substract the >>>below BOLDED values,your help is appreciated. >>> >>> >>>� >>>>� >>>>VAR1��������� VAR2���� WW1�� WW2� WW3�� >>>WW4� WW5� substraction >>>>-------------------------------------------------------------------------- >> - >>>------ >>>>WW1���������� WW2������ >>>1���������� 3��������� >>>1�������� 2�������� >>>�4�������������. >>>>WW2���������� WW1������ >>>3���������� 3�������� >>>�4�������� 3�������� >>>�4�������������. >>>>WW3���������� WW4������ >>>3���������� 2�������� >>>�3�������� 3�������� >>>�4�������������. >>>>WW4���������� WW3������ >>>4���������� 2�������� >>>�4������� �3�������� >>>�5������������ . >>>>WW5���������� WW2������ >>>4���������� 2�������� >>>�2�������� 4�������� >>>�5������������ . >>>> >>>>� >>>>� >>>>FORMULA >>>>----------- >>>>WW1-WW2� 1-3 =-2 >>>>WW2-WW1� 3-3 =0 >>>>WW3-WW4� 3-3 =0 >>>>WW4-WW3� 3-4 =-1 >>>>WW5-WW2� 5-2=-3 >>>>� >>>>FINAL OUTPT I WANT LIKE BELOW FROMAT >>>>-------------------------------------------------------- >>>>VAR1��������� VAR2���� WW1�� WW2� WW3�� >>>WW4� WW5� substraction >>>>-------------------------------------------------------------------------- >> - >>>------ >>>>WW1���������� WW2������ >>>1���������� 3��������� >>>1�������� 2�������� >>>�4�������������2 >>>>WW2���������� WW1������ >>>3���������� 3�������� >>>�4�������� 3�������� >>>�4�������������0 >>>>WW3���������� WW4������ >>>3���������� 2�������� >>>�3�������� 3�������� >>>�4�������������0 >>>>WW4���������� WW3������ >>>4���������� 2�������� >>>�4������� �3�������� >>>�5������������ 1 >>>>WW5���������� WW2������ >>>4���������� 2�������� >>>�2�������� 4�������� >>>�5������������ 3 >>>> >>>> >> >


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