Help using Base SAS procedures

converter numeric variable to character bigger than 30

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

converter numeric variable to character bigger than 30

Hi all, in order to convert a numeric variable to character WITH LENGTH=30 I use the function PUT:

VARC=PUT(VARN,30.);

but my problem appears when I try to convert a variable with length=40, or bigger:

VARC=PUT(VARN,50.);...it gives me an error of width

any help?

Thanks in advance,

V.


Accepted Solutions
Solution
‎06-04-2013 11:55 AM
Super User
Posts: 5,085

Re: converter numeric variable to character bigger than 30

Well, this would work:

data want;

set have;

length varc $ 50;

varc = right(put(varn, 15.));

run;

But the answer is that's the way SAS built the w.d format.  Maximum width is 32, per the documentation:

http://support.sas.com/documentation/cdl/en/leforinforref/63324/HTML/default/viewer.htm#n1n7bmvs1brl...

View solution in original post


All Replies
Super User
Posts: 5,085

Re: converter numeric variable to character bigger than 30

You have to realize that your data values are probably incorrect.  SAS does not accurately store numbers beyond 15 or 16 significant digits.  Here is a program you can run to illustrate:

data _null_;

x=12345678901234567890;

put x 20.;

run;

With that in mind, is your original question something that you need to solve or do you need to rethink an earlier part of the process?

Super Contributor
Posts: 301

Re: converter numeric variable to character bigger than 30

***It is fine;

data new;
varn=3;
varc=compress(put(varn,30.));
run;

***It is not;


data new2;
varn=3;
varc=compress(put(varn,40.));
run;

ERROR 29-185: Width specified for format F is invalid.

Q is....How can obtain varc with LENGHT = 40 or superior.

I hope it is more clear now, Thanks.

Super User
Posts: 5,085

Re: converter numeric variable to character bigger than 30

OK, this won't solve the problem if it turns out that your data values are already incorrect.  But it will convert them:

data want;

set have;

length varc $ 50;

varc = varn;

varc = left(varc);

run;

You can always omit the LEFT function if you prefer.  But don't combine into one statement.  You'll get scientific notation if you try:  varc = left(varn);

Good luck.

Super Contributor
Posts: 301

Re: converter numeric variable to character bigger than 30

Thanks for that, but still not convince with your answer, sorry, my Q is , why can't do it with the PUT function, this my question Smiley Happy. Thanks.

Super Contributor
Posts: 301

Re: converter numeric variable to character bigger than 30

This is like I want to achieve, as you can see, I need the length superior to 30, because the lenght need to match to add this extraline with PROC APPEND Smiley Happy

data new;
varn=3;
varc=compress(put(varn,30.));
run;


data new2;
varn=3;
varc=compress(put(varn,40.));
run;

data extra; length varc $30.;retain varc "Numbers of subjects living in the island of the Pacific Ocean" varn 0 ;run;  /* 30 is not enoguh */
proc append base=new data=extra;run;

Solution
‎06-04-2013 11:55 AM
Super User
Posts: 5,085

Re: converter numeric variable to character bigger than 30

Well, this would work:

data want;

set have;

length varc $ 50;

varc = right(put(varn, 15.));

run;

But the answer is that's the way SAS built the w.d format.  Maximum width is 32, per the documentation:

http://support.sas.com/documentation/cdl/en/leforinforref/63324/HTML/default/viewer.htm#n1n7bmvs1brl...

Super Contributor
Posts: 301

Re: converter numeric variable to character bigger than 30

Thanks Smiley Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 3369 views
  • 3 likes
  • 2 in conversation