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 (June 2007, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Fri, 15 Jun 2007 01:46:34 -0700
Reply-To:   barry.debenham@TALK21.COM
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   barry.debenham@TALK21.COM
Organization:   http://groups.google.com
Subject:   Re: dropped leading zeros converting from char to num
Comments:   To: sas-l@uga.edu
In-Reply-To:   <1181867678.406181.140340@z28g2000prd.googlegroups.com>
Content-Type:   text/plain; charset="us-ascii"

On 15 Jun, 01:34, hox...@gmail.com wrote: > On Jun 14, 6:27 pm, Reeza <fkhurs...@hotmail.com> wrote: > > > > > > > On Jun 14, 3:00 pm, hox...@gmail.com wrote: > > > > On Jun 14, 3:30 pm, datan...@GMAIL.COM ("data _null_;") wrote: > > > > > code=CATS(vvalue(m1),vvalue(m2),vvalue(m3)); > > > > > On 6/14/07, hox...@gmail.com <hox...@gmail.com> wrote: > > > > > > On Jun 14, 2:54 pm, Reeza <fkhurs...@hotmail.com> wrote: > > > > > > On Jun 14, 12:48 pm, hox...@gmail.com wrote: > > > > > > > > Hey, everyone. I'm trying to convert a three-digit char variable that > > > > > > > contains only three digits into a three-digit num variable in a SAS > > > > > > > 9.1 data step. When I make the conversion using something like > > > > > > > > data info2; set info1; > > > > > > > num1 = input(char1, 3.); > > > > > > > > it converts numbers >100 without a problem. However, something like > > > > > > > "008" gets converted to "8", and I'm interested in having "008" as the > > > > > > > num variable value. How can I prevent SAS from dropping these leading > > > > > > > zeros converting from a char to a num? Or, conversely, is there any > > > > > > > easy way to add the zeros back on after the conversion? Thanks! > > > > > > > Numerically, its the same value, but for printing or display purposes > > > > > > try the z3. format. > > > > > > > HTH, > > > > > > Reeza > > > > > > That worked very well, thank you. Now the question becomes how do I > > > > > concatenate a few of these variables together while keeping those > > > > > zeroes? For example, I'd like to combine m1=007, m2=082, and m3=042 > > > > > to get code=007082042 > > > > > > code=CATS(m1,m2,m3); > > > > > > seems to drop the leading zeros again, producing 78242. Any ideas? > > > > The reason why this might seem like it's more complicated than it > > > needs to be is because the 3-digit values had to be in ascending from > > > least to greatest before they could be concatenated. To further > > > complicate things, I'm working with a large data set and each > > > observation has either 1, 2, 3, or 4 of these three-digit codes, not > > > in any particular numeric order, stored in code1..code4 as char > > > variables (and empty values where the observation had less than 4 > > > codes). > > > > I converted code1..code4 to into code_num1..code_num4 (numeric > > > variables) and then wrote code to: > > > A - determine and store how many codes each observation had in a new > > > numeric variable and, > > > B - sort code_num1..code_numX into sort1..sort4, 4 numeric variables > > > that had the lowest code_num value in sort1 and the greatest code_num > > > value in sortX, where X is the number of codes the observation had. > > > > Once sort1..sortX contained the numbers in ascending order, I needed > > > to concatenate those and end up with a 3,6,9,or 12 digit final code to > > > process. Thus, I converted them into numeric variables to put them in > > > order. I've been thinking about it all afternoon, and I don't think > > > it matters whether the final result is a number or a character, > > > assuming I can still sort char variables containing only numbers as > > > though they're numbers. Thanks for your help, everyone! > > > As someone who's worked with such data, store it as a character, make > > it easy for someone to NOT make a mistake!!. > > You could have used array processing with min/max and median function > > for this, with different types of median to give the 2 and 3 values > > respectively, and simply checked for missing in your array before > > concatenating. > > min (array)=first, max(last) handles 1/2 median(array) handles case 3, > > different median calcs (3 or 4). > > > Not sure its more effective than what you have though, or will even > > work, just an idea. > > > HTH, > > Reeza > > > Reeza > > These are all great ideas, and I'm going to see what I can do first > thing Monday morning. Unfortunately, I'm not going to be back at the > office until then, nor do I have a copy of my code with me, but I'll > keep thinking about it this weekend, try some different approaches on > Monday morning, and let you all know what seems to work. Thanks again > for all the insightful replies so quickly!- Hide quoted text - > > - Show quoted text -

Informatting the character values into numeric variables removes the leading zeroes which are meaningless in arithmetic.

The easiest option is to concatenate the three character variables before you convert them into numeric. I have however shown how to convert them right back so that the leading zeroes can be included and concatenated when you want them.

data a; /* character variables */ m1c='003'; m2c='004'; m3c='005'; /* Convert to numeric variables */ m1=input(m1c,3.); m2=input(m2c,3.); m3=input(m3c,3.); /* Concatenate character and numeric variables */ coden=CATS(m1,m2,m3); codec=CATS(m1c,m2c,m3c); /* Concatenate converted numeric variables using Z3. format */ codew=CATS(put(m1,z3.),put(m2,z3.),put(m3,z3.)); /* Show the results in the log */ put coden= codec= codew= m1= m2= m3= m1c= m2c= m3c=; run;

The log reads as follows:

coden=345 codec=003004005 codew=003004005 m1=3 m2=4 m3=5 m1c=003 m2c=004 m3c=005

I hope that you can find something useful in there.

Regards,

Barry D.


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