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
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.
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;
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
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 .
@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.
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.
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;
@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.
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 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.