DATA Step, Macro, Functions and more

convert all numeric variables to character using array and vice versa

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

convert all numeric variables to character using array and vice versa

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


Accepted Solutions
Solution
‎02-17-2017 09:53 AM
Contributor
Posts: 69

Re: convert all numeric variables to character using array and vice versa

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


All Replies
Super User
Posts: 6,948

Re: convert all numeric variables to character using array and vice versa

What for?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,516

Re: convert all numeric variables to character using array and vice versa

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;

Contributor
Posts: 69

Re: convert all numeric variables to character using array and vice versa

[ Edited ]

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

Contributor
Posts: 24

Re: convert all numeric variables to character using array and vice versa

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 .

Super User
Posts: 10,516

Re: convert all numeric variables to character using array and vice versa


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.

Solution
‎02-17-2017 09:53 AM
Contributor
Posts: 69

Re: convert all numeric variables to character using array and vice versa

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.

Contributor
Posts: 30

Re: convert all numeric variables to character using array and vice versa

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;

 

Super User
Posts: 10,516

Re: convert all numeric variables to character using array and vice versa


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.

Contributor
Posts: 30

Re: convert all numeric variables to character using array and vice versa

Sorry. It was CSV in my working code, would not work otherwise. Bad habit to copy/paste half-baked code.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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