Hello,
I'm trying to convert my character variables which are more than 4000 to numeric and I'd like to also get their labels assigned to new numeric variables.
I found attached PDF document for this purpose, it's working for a few variables but since I have more than 4000 variables, it's giving error.
I was wondering if anyone has better solutions or idea to resolve this problem ?
Thank you.
You do not have to use macro logic to generate code. You can use regular SAS code.
Assume you have dataset named CONTENTS with the output from PROC CONTENTS for your variables.
filename code temp;
data _null_;
set contents ;
file code ;
temp = cats('_',_n_);
put temp '=input(' name ',' length 5. '.);'
/ 'rename ' temp '=' name ';'
/ 'drop ' name ';'
;
if label ne ' ' then put
'label ' temp '=' label :$quote. ';'
;
run;
data want ;
set have ;
%inc temp;
run;
Message was edited: Fixed DROP statement.
What is your situation, exactly? 4000 variables looks to be a lot there must be some source of it. Rows / cols ?
Where is your data stores SAS or in RDBMS?
Are there limitiations that are precision related (flosting point) is binning an approach?
The approach of working with arrray-s can be popssible modified and improved for your situation.
The original source is txt file, I loaded that into SAS but now I need to convert the variables to numeric
overcome the issue of macro var length by just using proc sql; with macro facility to create many variables instead
proc sql;
select name, quote(trim(label))
into :vname1-:vname9999, :vlabel1-:vlabel9999 /*It is possible you may need to break this down into 2 select statements, I don't remember if into statement supports range over multiple macro variables */
from sashelp.vcolumn
where libname = "WORK" and
memname = "HAVE" and
type = "char"
/* and any other condition to discriminate the "right" character variables */
;
quit;
%macro magic();
data want;
set have;
%do i=1 %to &sqlobs;
new_&&vname&i.. = input(&&vname&i.. , best32.);
%end;
label
%do i=1 %to &sqlobs;
new_&&vname&i.. = &&vlabel&i..
%end;
; /* closing label statements' semi column */
/* add another do loop to drop &&vname&i.. once you are satisfied with test results */
run;
%mend;
%magic();
Vincent
You do not have to use macro logic to generate code. You can use regular SAS code.
Assume you have dataset named CONTENTS with the output from PROC CONTENTS for your variables.
filename code temp;
data _null_;
set contents ;
file code ;
temp = cats('_',_n_);
put temp '=input(' name ',' length 5. '.);'
/ 'rename ' temp '=' name ';'
/ 'drop ' name ';'
;
if label ne ' ' then put
'label ' temp '=' label :$quote. ';'
;
run;
data want ;
set have ;
%inc temp;
run;
Message was edited: Fixed DROP statement.
Don't you need to drop NAME not TEMP.
Yes.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.