BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
draroda
Fluorite | Level 6

Hi all,

 

I want to convert all numeric data to character and and all character vars to numeric using arrray.

 

Can anyone please help me.

 

Regards,

Rajesh

1 ACCEPTED SOLUTION

Accepted Solutions
cau83
Pyrite | Level 9

If you're looking for a universal solution (any number of character variables) SAS support has this suggestion: http://support.sas.com/kb/40/700.html

 

If it's only 4 variables the repeating syntax does not seem like a big deal. i'm assuming you are dealing with more.

View solution in original post

9 REPLIES 9
ballardw
Super User

If your intent is to replace the contents in the existing variables then that is not going to happen. SAS numeric variables are just that, numeric. You cannot place text into a numeric. And numeric values will get changed to text when placed into a character variable.

 

You will need to provide a very explicit example of some start data and what the result should look like with what you are attempting to do.

 

Note that you can associate a display format with values so the result looks like character;

 

Proc format;

value yn

1= 'Yes'

0= 'No';

run;

data junk;

   input x;

datalines;

1

0

;

run;

proc print data=junk;

   var x;

   format x yn.;

run;

cau83
Pyrite | Level 9

In general, define array for all character (array ARRNAME _character_; )

and then loop through all of the variables (do over ARRNAME; )

 

but some more info on what/why would be helpful

draroda
Fluorite | Level 6

I have variables a b c d with numbers values but they are of character type variable and i do not want to write any syntax repeatedly.

 

Thats why i am lookng for any macro code which can convert all character type variables to numeric variables .

ballardw
Super User

@draroda wrote:

I have variables a b c d with numbers values but they are of character type variable and i do not want to write any syntax repeatedly.

 

Thats why i am lookng for any macro code which can convert all character type variables to numeric variables .


Maybe it is time to go back in your process and determine why those variables that you want to be numeric were created as character to begin with. Did you by any chance use PROC Import to bring the data into SAS? While helpful the procedure has to guess what type a variable may be and some common conditions, such as the value for a column being missing in the first rows of the data or non-numeric codes such as NULL or characters such a () surrounding the digits will be assumed to be character.

 

Addressing such things early often simplifies things later.

cau83
Pyrite | Level 9

If you're looking for a universal solution (any number of character variables) SAS support has this suggestion: http://support.sas.com/kb/40/700.html

 

If it's only 4 variables the repeating syntax does not seem like a big deal. i'm assuming you are dealing with more.

ErikLund_Jensen
Rhodochrosite | Level 12

If I understand you correctly, you have a dataset with character variables, but numeric content, and you want to convert these to numeric varables, but with the same variable names.

A quick and dirty solution is to export data to CSV and import again, the import procedure vill use the same names, but determine variable type from the content:

 

data test;

input a$ b c$ d e$ f g$ h i$ j k l m n;

cards;

1 2 A 4 5 6 B 8 9 0 1 H 3 4

;

run;

proc export data=test dbms=csv outfile="e:\work\test.xlsx" replace;

run;

proc import datafile="e:\work\test.xlsx" dbms=csv out=test2 replace;

run;

 

ballardw
Super User

@ErikLund_Jensen wrote:

If I understand you correctly, you have a dataset with character variables, but numeric content, and you want to convert these to numeric varables, but with the same variable names.

A quick and dirty solution is to export data to CSV and import again, the import procedure vill use the same names, but determine variable type from the content:

 

data test;

input a$ b c$ d e$ f g$ h i$ j k l m n;

cards;

1 2 A 4 5 6 B 8 9 0 1 H 3 4

;

run;

proc export data=test dbms=csv outfile="e:\work\test.xlsx" replace;

run;

proc import datafile="e:\work\test.xlsx" dbms=csv out=test2 replace;

run;

 


Very  bad habit to name CSV files with an XLSX extension.

 

Use a large value for GUESSINGROWS in the Import step as you may introduce or repeat character varaibles for missing values.

ErikLund_Jensen
Rhodochrosite | Level 12
Sorry. It was CSV in my working code, would not work otherwise. Bad habit to copy/paste half-baked code.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 5431 views
  • 1 like
  • 5 in conversation