```Date: Thu, 24 Sep 2009 11:14:08 -0700 Reply-To: "Huang, Ya" Sender: "SAS(r) Discussion" From: "Huang, Ya" Subject: Re: programming problem Comments: To: oloolo In-Reply-To: <200909241759.n8OHDJmB019528@malibu.cc.uga.edu> Content-Type: text/plain; charset="us-ascii" Out of those 729, some are already ordered correctly, such as 123,256,467 etc. We just need to find those number ordered incorrectly, such as 657,32 etc. then use them to construct the informat. For those already ordered correctly, we don't have to list them in the invalue statement, they are by default mapped to their original value. Of course this will take some effort. But it could be done with SAS. -----Original Message----- From: oloolo [mailto:dynamicpanel@YAHOO.COM] Sent: Thursday, September 24, 2009 11:00 AM To: SAS-L@LISTSERV.UGA.EDU; Huang, Ya Subject: Re: programming problem you need to set up 9^3=729 distinct values On Thu, 24 Sep 2009 13:01:35 -0400, Ya Huang wrote: >Talking about lookup table, since the original number has only 3 >digits, maybe the most efficient way is to create a informat (less then 999 lines): >Something like this: > >data have; > input old; > cards; >32 >12 >576 >2 >54 >; > >proc format; >invalue s >'576'=567 >'54'=45 >'32'=23 >; > >data need; > set have; >new=input(compress(put(old,best3.)),s.); >run; > >proc print; >run; > > Obs old new > > 1 32 23 > 2 12 12 > 3 576 567 > 4 2 2 > 5 54 45 > > >On Thu, 24 Sep 2009 03:43:13 -0400, Keintz, H. Mark > wrote: > >>Cornel: >> >>You have received a nice code example for "sorting" the digits in a >>multi- >digit number. It runs call sortc for every incoming observations (see >code at bottom). >> >>But if you have a lot of observations, you may want to use a lookup >table. The example below runs call sortc only 1,000 times, regardless >of the size of the incoming dataset. >> >> >>%let nd=3; %* Number of digits **; >> >>data want; >> array lookup {0:%sysevalf(1e&nd-1,integer)} \$&nd _temporary_; >> array dig{&nd} \$1 _temporary_ ; >> length _numtext \$&nd ; >> >> if _n_=1 then do _i= 0 to dim(lookup)-1; >> _numtext=put(_i,&nd..); >> do _d = 1 to &nd; >> dig{_d}=substr(_numtext,_d,1); >> end; >> call sortc(of dig[*]); >> lookup{_i}=cat(of dig[*]); >> end; >> drop _: ; >> >> set have; >> length sort_num \$&nd ; >> sort_num=lookup{orig_num}; >>run; >> >> >>For testing purposes, I created a dataset of one million observations. >> >>data have; >> do n=1 to 1e6; >> orig_num=floor(999*uniform(0980865)); >> output; >> end; >>run; >> >> >>Using this test dataset, the DATA WANT step above took 0.3 seconds . >>The >step below took 3.3 seconds. I guess you'd have to have a slow system, >or a LOT more data for this difference to matter. But it is >illustrative of the potential advantages of creating a lookup array (or >hash object in other circumstances) in the first iteration (_N_=1) of >the DATA step and then using that lookup table (or object) for all the >incoming data. This is a concept regularly suggested by Paul Dorfman and others on SAS-L. >> >>data want (keep=orig_num new); >> set have; >> array test (3) \$; >> do i=1 to length(trim(left(put(orig_num,best12.)))); >> test(i)=substr(trim(left(put(orig_num,best12.))),i,1); >> end; >> call sortc (of test[*]); >> new=catt(of test[*]); >>run; >> >> >>> -----Original Message----- >>> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of >>> Cornel Lencar >>> Sent: Wednesday, September 23, 2009 6:01 PM >>> To: SAS-L@LISTSERV.UGA.EDU >>> Subject: programming problem >>> >>> Hi, >>> I have a numerical variable represented by a number that can be up >>> to three digits long. I would like to create a new variable based on >>> the old variable such that the digits are sorted in increasing (or >>> decreasing) order, such as: >>> >>> OLD_VARIABLE NEW_VARIABLE >>> 32 23 >>> 12 12 >>> 576 567 >>> 2 2 >>> 54 45 >>> .. .. >>> >>> Any ideas would be greately appreciated. >>> >>> Sincerely, >>> >>> Cornel Lencar ```

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