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 2009, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sat, 23 May 2009 05:17:12 +0800
Reply-To:     Huijuan Meng <huijuan_meng@HOTMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Huijuan Meng <huijuan_meng@HOTMAIL.COM>
Subject:      Re: Convert numeric to character,
              but don't know the length of the numeric variable.
Comments: To: mterjeson@russell.com
In-Reply-To:  <16FD64291482A34F995D2AF14A5C932C07437DFF@MAIL002.prod.ds.russell.com>
Content-Type: text/plain; charset="Windows-1252"

Thank you so much for your help, sorry I did not explain my question more specific.

Here is my situation:

I have a dataset which contain, say, 1100 variables,

I run some analysis with this data, based on the results, I need to delete certain variables,

Then I need to find the number of the rest of the variables, it can be >=1000, or < 1000,

Finally, I need to write a text file which referring to this number a couple of times, and I don't want any space between this number and other words in the text file.

Now I have a global macro variable:

%global c1n;

In my macro, I have

data t1;

dsid = open ("mydata");

c1n0 = attrn (dsid,'nobs');

c1n=put(c1n0,4.0);

run;

proc sql noprint;

select c1n into :c1n from t1;

quit;

data textfile;

file "C:\textfile.txt";

@1 ">INAmes = (Q1(1)Q" "&c1n." ");"/

@1 ">INUmber = (1(1)" "&c1n." ");"/

;

RUN;

So if c1n=999, I want my text file looks like:

>INAmes = (Q1(1)Q999);

>INUmber = (1(1)999);

Instead of

>INAmes = (Q1(1)Q 999);

>INUmber = (1(1) 999 );

I hope that makes sense.

Thanks a lot.

Huijuan Meng

> Subject: RE: Convert numeric to character, but don't know the length of the numeric variable. > Date: Fri, 22 May 2009 13:56:54 -0700 > From: Mterjeson@russell.com > To: huijuan_meng@HOTMAIL.COM; SAS-L@LISTSERV.UGA.EDU > > Hi Huijuan Meng, > > Yes, that can be done. > How we (SAS-L providing solutions) > and you (needing the desired output) > achieve that is going to vary unless > we fine-tune the criteria of your > actual need. > > Your description sounds reasonable if > you only have one observation you are > dealing with. More common in SAS we > have more than one observation. i.e. > if we determine that the length for > the variable's value for row 1 is 3 > for your value of 156, does that mean > that all of the rest of the observations > for that variable are also going to be > 3 digits no less and no more? You can > see why we would ask that. In light of > that type of complication, I am wondering > if the length of the storage string really > needs to handle the largest length for the > storage aspect and then you usage/need of > how you want to use the value later may > be of much more importance. Yes, the > trailing part of a string variable will > be padded with spaces. If your need to > have it only be 3 characters for the value > of 156 is due to a display requirement or > a concatenation task into another string > result, then it is more common to deal with > the different length issue at that time. > e.g. you have > obs 1 may have var = '1560' > obs 2 may have var = '156 ' > > To display only the valid digits, or to > concatenate into another string without > the trailing space character, the solution > is the same > > trim(var) > or > trim(left(var)) > or > compress(var) > > will yield '1560' and '156' when rendered, > even though the storage still remains as > obs 1 may have var = '1560' > obs 2 may have var = '156 ' > > > You can find the length in various ways > such as > > data _null_; > x = 156; > len_of_x = length(trim(left(put(x,best.)))); > put _all_; > run; > > but I am not sure that's really the direction > your better-addressed-issue would take you. > > > > So the real question is, do you really need the > storage for all rows for that variable set to a > specified length and ALL values in the ALL rows > will be exactling the same length of characters? > Or do all your rows for that variable need enough > storage to contain all the possible values and > you just need to work with the exact length when > working with each unique content value? These > would be handled differently. > > > > > Hope this is helpful. > > > Mark Terjeson > Investment Business Intelligence > Investment Management & Research > Russell Investments > 253-439-2367 > > > Russell > Global Leaders in Multi-Manager Investing > > > > > > > -----Original Message----- > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of > Huijuan Meng > Sent: Friday, May 22, 2009 1:32 PM > To: SAS-L@LISTSERV.UGA.EDU > Subject: Convert numeric to character, but don't know the length of the > numeric variable. > > Hi all, > > I want to convert a numeric variable (e.g., 1560) to a character > variable, I can use put (num,4.0) to nake a $4 variable, but if I have > 156, that would create an extra space in the character variable, is > there anyway that I can use to find the number of digit in the numeric > variable and convert it using corresponding format? > > Thanks. > > > > _________________________________________________________________ > Invite your mail contacts to join your friends list with Windows Live > Spaces. It's easy! > http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.a > spx&mkt=en-us

_________________________________________________________________ Windows Live™: Keep your life in sync. Check it out! http://windowslive.com/explore?ocid=TXT_TAGLM_WL_t1_allup_explore_012009


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