- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I am relatively new to SAS programming so any help/advice is MUCH appreciated. I have a dataset that has approximately 925 variables that are currently character variables and need to be converted into numeric variables. The values within each of the variables are already numbers (1s and 0), so I just need to convert the variable type to numeric.
Any suggestions on how best convert these variables to numeric all at once? I was trying to do an array and just cannot seem to get it to work.
Thank you in advance!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Show us how you would (1) convert a single variable, then (2) how you might do an array of two variables to facilitate the conversion. And please provide some sample data in a working data step.
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set
Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets
--------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
When converting a single variable I typically do it like so:
num_var=input(char_var,8.);
drop char_var;
rename num_var;
This is the current code a friend offered (and honestly is a bit over my head) that I am attempting to use but am getting some errors:
%Macro Numb;
%let excludevars=id;
proc sql noprint;
select name into :charvars separated by ' '
from dictionary.columns
where libname="WORK" and memname="Endorse_OTP_V1" and type="char" and name like "t_%"
and not indexw(upcase("&excludevars"),upcase(name));
quit;
%let ncharvars=%sysfunc(countw(&charvars));
%put charvars=&charvars;
%put ncharvars=&ncharvars;
data _null_;
set endorse_otp_v1 end=lastobs;
array charvars{*} &charvars;
array charvals{&ncharvars};
do i=1 to &ncharvars;
if input(charvars{i},?? best12.)=. and charvars{i} ne ' ' then charvals{i}+1;
end;
if lastobs then do;
length varlist $ 32767;
do j=1 to &ncharvars;
if charvals{j}=. then varlist=catx(' ',varlist,vname(charvars{j}));
end;
call symputx('varlist',varlist);
end;
run;
%put varlist=&varlist;
%let nvars=%sysfunc(countw(&varlist));
%macro renamer;
%do i=1 %to &nvars;
rename x&i = %scan(&varlist,&i) ;
%end;
%mend renamer;
data endorse_otp_v1;
set endorse_otp_v1;
array charx{&nvars} &varlist;
array x{&nvars} ;
do i=1 to &nvars;
x{i}=input(charx{i},best12.);
end;
drop &varlist i;
%renamer
run;
%end;
%mend;
%numb;
:
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Is there any possibility of going back to source and rereading these variables correctly in the first place? This would be far better than band-aiding afterwards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
When I read something like
@dgaribal wrote:
approximately 925 variables
The back of my mind immediately starts to scream "DESIGN FAILURE!".
One way to easily convert is this:
data have;
input id $ (A B C) ($);
datalines;
A 1 0 1
B 0 0 1
;
proc transpose
data=have
out=long
;
by id;
var a--c;
run;
data long2;
set long;
numvar = input(col1,best.);
run;
proc transpose
data=long2
out=want (drop=_name_)
;
by id;
id _name_;
var numvar;
run;
But you should seriously contemplate if the long layout (dataset LONG2) isn't better in the long run.