thousands of numeric variables convert to charector variables

thousands of numeric variables convert to charector variables

Post by chendlca » Sat, 27 Aug 2005 09:47:19


Hi All,

I know how to convert limited numeric variables to
charector variables. But I could not think of a easy
way to convert thousands of numeric variables to
charector variables.


data a; set b;
array n (*) a1--a1000 b200--b590;
do i=1 to dim(n);
x=put(n(i), $best.);
call symput ('char',x);
end; drop i;
end;



I used call symput functionhere, not sure if I should
it here. but from here, I don't know how to deliver
the 'char' to the original var name, which was defined
as numeric var.

I will appreciate your help !

Dan





____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yqcomputer.com/
 
 
 

thousands of numeric variables convert to charector variables

Post by Vladisla » Sat, 27 Aug 2005 17:59:48

I guess, if I know the final purpose of the data manipulation you are
doing, I would advise you some more practical way. However here is some
practical was to do exactly what you have specified.
You should use Proc option with out= option to generate several macro
variables, then to use them in the data step, performing transformation .

Here is the code, which proved to work in Open/Vms SAS8.2

/*
~ bType SAS
~ bName TMP010.SAS
~ bCategory Module
~ bCreated 26-AUG-2005
~ bBy V.Moltchanov
~ bPurpose converting all numerical variables to character ones
~ bInput &dsin - name of input SAS data set
~ bOutput &dsout- name of resulting SAS data set
~ bControl &pref - prefix for intermediate working names
~ bDetails temporary text files _rena_to.txt and _rena_fro.txt
in working version are to be deleted automatically.
~ LUsage


~>
*/

libname cvd "RISK_ROOT:[FR_PR.DM_CVD.WRK_KATOH]";
%let dsin=cvd.CHD_ALL;
%let pref=___;
%let dsout=d._dsout;


proc contents noprint out=cont(keep=name type length label format
formatl formatd npos)
data=&dsin;
run;
proc sort; by type;


*generating &rena_to &nvar_num &nvar_all;
data _null_; file '_rena_to.txt';
set cont end=eof;
length z $ 200;
if _N_=1 then put '%let rena_to=';
z=trim(name)!!"=&pref.x" !! left(put(_N_,6.));
nvar_num+(type=1); *count of numerical variables;
nvar_all+1; *count of all variables ;
if eof then do; put z ';';
call symput('nvar_num', left(put(nvar_num,6.)));
call symput('nvar_all', left(put(nvar_all,3.)));
end;
else put z;
run;
%inc '_rena_to.txt';
run;
*at this point macro var rena_to nvar_num and nvar_all are ready;
%put &nvar_num &nvar_all;


*generating &rena_fro;
data _null_; file '_rena_fro.txt';
set cont end=eof;
length z $ 200;
if _N_=1 then put '%let rena_fro=';
if type=1 then
z= "&pref.s" !! left(put(_N_,6.))!!"="!! trim(name);
else
z= "&pref.x" !! left(put(_N_,6.))!!"="!! trim(name);

if eof then put z ';';
else put z;
run;
%inc '_rena_fro.txt';

*at this point macro var rena_fro is ready;
ruN;


data &dsout (rename=(&rena_fro)); length &pref.s1- &pref.s&nvar_num $ 20;
set &dsin(rename=(&rena_to));
array x &pref.x1- &pref.x&nvar_num;
array s &pref.s1- &pref.s&nvar_num;
do i=1 to &nvar_num;
s[i]= put(x[i],best.);
end;
drop i &pref.x1- &pref.x&nvar_num;
run;
Proc contents;run;

 
 
 

thousands of numeric variables convert to charector variables

Post by Vladisla » Sat, 27 Aug 2005 18:13:24

Dan,
I am not sure that if I'll know the final purpose of your manipulations
with data, I would advise you something else.
However here is the code which does exactly what you have specified.
Some cleaning and variable re-arranging in the final data set is still
out of this code.

You shoud use the Proc contents with out= options to generate several
macrovariables, which later are sued in the data step, where
transofrmation is performed.
The code proved to work in Open VMS/SAS8.2
The libnames in the code should be replaced by yours specifications.


%let m_name=TMP010.SAS;
/*
~ bType SAS
~ bName TMP010.SAS
~ bCategory Module
~ bCreated 26-AUG-2005
~ bBy V.Moltchanov
~ bPurpose converting all numerical variables to character ones
~ bInput &dsin - name of input SAS data set
~ bOutput &dsout- name of resulting SAS data set
~ bControl &pref - prefix for intermediate working names
~ bDetails temporary text files _rena_to.txt and _rena_fro.txt
in working version are to be deleted automatically.
~ LUsage


~>
*/

libname cvd "RISK_ROOT:[FR_PR.DM_CVD.WRK_KATOH]";
%let dsin=cvd.CHD_ALL;
%let pref=___;
%let dsout=d._dsout;


proc contents noprint out=cont(keep=name type length label format

formatl formatd npos)
data=&dsin;
run;
proc sort; by type;


*generating &rena_to &nvar_num &nvar_all;
data _null_; file '_rena_to.txt';
set cont end=eof;
length z $ 200;
if _N_=1 then put '%let rena_to=';
z=trim(name)!!"=&pref.x" !! left(put(_N_,6.));
nvar_num+(type=1); *count of numerical variables;
nvar_all+1; *count of all variables ;
if eof then do; put z ';';
call symput('nvar_num', left(put(nvar_num,6.)));
call symput('nvar_all', left(put(nvar_all,3.)));
end;
else put z;
run;
%inc '_rena_to.txt';
run;
*at this point macro var rena_to nvar_num and nvar_all are ready;
%put &nvar_num &nvar_all;


*generating &rena_fro;
data _null_; file '_rena_fro.txt';
set cont end=eof;
length z $ 200;
if _N_=1 then put '%let rena_fro=';
if type=1 then
z= "&pref.s" !! left(put(_N_,6.))!!"="!! trim(name);
else
z= "&pref.x" !! left(put(_N_,6.))!!"="!! trim(name);

if eof then put z ';';
else put z;
run;
%inc '_rena_fro.txt';

*at this point macro var rena_fro is ready;
ruN;


data &dsout (rename=(&rena_fro)); length &pref.s1- &pref.s&nvar_num

$ 20;
set &dsin(rename=(&rena_to));
array x &pref.x1- &pref.x&nvar_num;
array s &pref.s1- &pref.s&nvar_num;
do i=1 to &nvar_num;
s[i]= put(x[i],best.);
end;
drop i &pref.x1- &pref.x&nvar_num;
run;
Proc contents;run;
 
 
 

thousands of numeric variables convert to charector variables

Post by Richard A. » Mon, 29 Aug 2005 14:59:44


data foo;
do row = 1 to 10;
array a[4000] ;
array b[2000] ;
OUTPUT;
end;
run;

data _null_;
if 0 then set foo(obs=0);

array n a1--a1000 b200--b590;

call execute ('data bar; set foo;');
do i = 1 to dim (n);
vname = vname(n[i]);
call execute (cats(vname,'C=put(',vname,',best12.);'));
call execute (cats('drop '||vname,';'));
call execute (cats('rename '||vname,'C=',vname,';'));
end;
call execute ('run;');
stop;
run;

The column order of "bar" will be different than that of "foo". Thus the
use of the double dash (--) variable selector with "bar" should be carefully
considered.
What is the impetus for converting only part of an array (b200--b590) to
char ?

--
Richard A. DeVenezia
http://www.yqcomputer.com/