Date: Thu, 2 Jul 1998 16:30:03 +0200
Reply-To: ANavodnik@SRC.SI
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Andrej Navodnik <ANavodnik@SRC.SI>
Subject: Re: character to numeric
Content-type: text/plain; charset=us-ascii
Andrej Navodnik@SRC
02.07.98 16:30
Hi Lynn,
I have used the idea from Jack Hamilton <jack_hamilton@HCCOMPARE.COM> and
wrote a macro to change character variables to numeric. You can also
specify a list of character variables which should not be changed using
exclude parameter. All character variables should be in such format that
can be read using BEST. informat.
data a;
length a1 a2 a3 a4 $ 8 n1 n2 8;
a1='1';
a2='2';
a3='3';
a4='4';
n1=1;
n2=2;
run;
data x; set a;
options nomprint nomtrace;
/*
Description:
Convert character variables to numeric
Usage:
ds_in - input dataset
ds_out - output dataset
exclude - list of character variables which should not be transformed
*/
%macro convert(ds_in, ds_out, exclude);
%let ds_in = %upcase(&ds_in);
%let exclude = %upcase(&exclude);
%if %sysfunc(exist(&ds_in,data)) gt 0 %then %do;
%let dsid = %sysfunc(open(&ds_in,i));
%if &dsid gt 0 %then %do;
%let firstvar = 1;
%let nvars = %sysfunc(attrn(&dsid,NVARS));
proc sql;
create table &ds_out as
select
%do i = 1 %to &nvars;
%let varname = %sysfunc(varname(&dsid,&i));
%let index = %sysfunc(index(&exclude,%upcase(&varname)));
%if %upcase(%sysfunc(vartype(&dsid,&i)))=C and &index = 0 %then
%do;
%if &firstvar = 1 %then %do;
input(&varname,best.) as &varname
%let firstvar = 0;
%end;
%else %do;
, input(&varname,best.) as &varname
%end;
%end;
%else %do;
%if &firstvar = 1 %then %do;
&varname as &varname
%let firstvar = 0;
%end;
%else %do;
, &varname as &varname
%end;
%end;
%end;
%let rc=%sysfunc(close(&dsid));
from &ds_in; quit;
%end;
%else %do;
%put ERROR: Dataset &ds_in could not be opened.;
%end;
%end;
%else %do;
%put WARNING: Dataset &ds_in does not exist.;
%end;
%mend;
/* change all character variables */
%convert(a,c);
/* change all character variables except variable a1 */
%convert(a,c,a1);
%convert(a,c,a3);
%convert(a,c,a3 a4);
%convert(a,c,a2 a3 a4);
/* change all variables, put the result in the same dataset */
%convert(x,x);
Kind regards,
Andrej
Andrej Navodnik
SRC INFO d.o.o.
Trzaska 18
1000 Ljubljana
Slovenia
e-mail: ANavodnik@src.si
Jack Hamilton <jack_hamilton@HCCOMPARE.COM>, datum: 01.07.98 18:50:58
Prejemnik: SAS-L@UGA.CC.UGA.EDU
V vednost: (tajno: Andrej Navodnik/SRC)
Zadeva: Re[2]: character to numeric
It would be easier to do that using PROC SQL. You could use a statement
of the form:
select input(abc, best.) as abc
which eliminates a lot of renaming. I think I'll put that in my WUSS
Coder's Corner paper about the uses of the data dictionary.
"Berryhill, Timothy" <TWB2@PGE.COM> wrote:
>No.
>Someone could write a macro to: 1) set the view SASHELP.VCOLUMN and get
the
>list of variables in the dataset, 2) write a input dataset rename option
to
>rename them all to temporary names, 3) define arrays of the original and
>tempory names (the "original" names should default to numerics so no
LENGTH
>statement required to force numeric), 4) write a DO loop and assignment
>statement to assign the temporary variables to the original variables (use
>the INPUT function or accept the notes about forced conversion), and 5)
>write an output dataset drop option to get rid of the temporary names. It
>would take a while to get it debugged and working.
Tim Berryhill - Contract Programmer and General Wizard
TWB2@PGE.COM or http://www.aartwolf.com/twb.html
Frequently at Pacific Gas & Electric Co., San Francisco
The correlation coefficient between their views and
my postings is slightly less than 0
> ----------
> From: Lynn Nicole Lethbridge[SMTP:lynnl@IS.DAL.CA]
> Reply To: Lynn Nicole Lethbridge
> Sent: Monday, June 29, 1998 10:51 AM
> To: SAS-L@UGA.CC.UGA.EDU
> Subject: character to numeric
>
> Hi there
>
> Is there a quick way in SAS to change all of the variables in my dataset
> (there are just over a hundred) from character variables to numeric
> variables.
>
> Thanks very much for your time.
>
> Lynn
>