BookmarkSubscribeRSS Feed
stephanicurry
Obsidian | Level 7

Hello!

 

How do I change my entire dataset from character to numeric??

 

I have the code on how to do one variable at a time but is there a code for the whole dataset?

 

Using SAS 9.4 for windowing environments!

 

Thanks!

3 REPLIES 3
PaigeMiller
Diamond | Level 26

You might want to consider changing the way the data set was created, rather than changing the variables. If the data was read from Excel or from a .csv file, then changing the manner of import (or changing the Excel file/.csv file itself) might be the best and easiest solution.

 

Otherwise, you'd have to write a macro that loops through all variables and creates new numeric variables based upon the old character variable values. For example: https://communities.sas.com/t5/General-SAS-Programming/How-to-convert-many-character-variables-to-nu...

--
Paige Miller
PGStats
Opal | Level 21

Here is one way using call execute:

 

data have;
a = "1"; b = "12.3"; c = "";
output;
run;

data _null_;
if 0 then set have;
array _c _character_;
length _l $200;
call execute ("data want; set have;"); 
do _i = 1 to dim(_c);
 	_l = cat("_", vname(_c{_i}), "=input(", vname(_c{_i}), ",best.);drop ", 
 		vname(_c{_i}), ";rename _", vname(_c{_i}), "=", vname(_c{_i}), ";");
 	call execute (_l);
	end;
call execute ("run;");
run;

It reads all your fields with the best. informat.

PG
Kurt_Bremser
Super User

Go back and revisit the process that got you in this mess. Depending on your data source, it can be very easy to fix this at the point where the data enters your SAS environment.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 550 views
  • 1 like
  • 4 in conversation