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
In-Reply-To: <BANLkTi=XT_33uPppnR_8GpkAcQQy+AbdaQ@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
First off, you can use
put(var_num,1.)
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
format differently.
a) If you really want the characters to be in the variable, do
put(var_num,charfmt.)
and define charfmt as a numeric format
value charfmt
1='A'
0='B'
other='Misscoded'
;
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:
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.
-Joe
On Mon, Jun 27, 2011 at 4:41 AM, abderrahim oulhaj <sas58147@gmail.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".
> To
> make it easy for you to understand my problem, I wrote this SAS code to
> show
> you the problem. The results are also given just below the code.
>
> Best wishes,
>
> Take_it_easy_58147
>
> */Proc format*/
>
> proc format;
> value $var_char '0' = 'A'
> '1' = 'B'
> ' ' = 'missing'
> other = 'miscoded';
> run;
>
> /*Data step */
>
> data df;
> input var_num 4.;
> var_char = put(var_num, 4. );
> datalines;
> 1
> 0
> 1
> ;
> run;
>
> /*proc print */
>
> proc print data = df;
> var var_char;
> format var_char $var_char.;
> run;
>
> /*Output*/
>
> var1_
> Obs var_char
>
> 1 miscoded
> 2 miscoded
> 3 miscoded
>
|