Date: Mon, 27 Jun 2011 06:54:11 -0500
Reply-To: Joe Matise <snoopy369@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Joe Matise <snoopy369@GMAIL.COM>
Subject: Re: PUT function
Content-Type: text/plain; charset=ISO-8859-1
First off, you can use
with the example code. If you actually have 4 long data, you can use the
strip/compress/trim options others have mentioned, or the -L option (so,
put(var_num,4. -L) to left align it.
Finally, I would say that most of the time you should use your user-defined
a) If you really want the characters to be in the variable, do
and define charfmt as a numeric format
or something like that.
b) Usually it's better not to store the character value at all. Simply
define the format as I describe above, and then in the data step, apply the
format var_num charfmt.;
and then it will always appear as 'A', 'B' etc. in procs and when you look
at the data [though be aware some methods of exporting do not preserve
formats]. That maintains the underlying value (the 1,0,etc.) but displays
the value labels.
On Mon, Jun 27, 2011 at 4:41 AM, abderrahim oulhaj <email@example.com>wrote:
> Dear SAS users,
> I am migrating from R to SAS. So you may consider i am a beginer in SAS. I
> am self studying it using some books. My question is a s follows:
> 1- In the data step, I converted a numeric variable called "var_num" to
> a character one called "var_char", using the PUT function.
> 2- I also defined a user-defined format for the "var_char" variable.
> The problem is that the format I defined is not working for "var1_char".
> make it easy for you to understand my problem, I wrote this SAS code to
> you the problem. The results are also given just below the code.
> Best wishes,
> */Proc format*/
> proc format;
> value $var_char '0' = 'A'
> '1' = 'B'
> ' ' = 'missing'
> other = 'miscoded';
> /*Data step */
> data df;
> input var_num 4.;
> var_char = put(var_num, 4. );
> /*proc print */
> proc print data = df;
> var var_char;
> format var_char $var_char.;
> Obs var_char
> 1 miscoded
> 2 miscoded
> 3 miscoded