DATA Step, Macro, Functions and more

Converting Numeric Column into Character Column by a macro

Reply
Occasional Contributor
Posts: 15

Converting Numeric Column into Character Column by a macro

Hi ,

Please help me in coding below logic:

Read the dataset and if dataset contains any numeric column then convert that column into character column.
Ex:
Dataset has two columns A and B.
A numeric 8 ;
B character 1 ;

so here Column A should become character column with length 256.

Please note that we dont have fixed columns meaning dataset is created in a seprate program which doest have fixed columns.

Many Thanks for your help.
Super Contributor
Super Contributor
Posts: 3,174

Re: Converting Numeric Column into Character Column by a macro

A DATA step should satisfy your rqmt here - after inputting SAS CHARACTER variables to start with an INPUT Statement, then using various SAS functions like ANYALPHA / ANYDIGIT to test your strings, followed by the INPUT function to convert, as required, from character to numeric. Lastly, consider the use of KEEP / DROP to only capture the SAS variables you require for the SAS output file/member/table.

Below are a few examples of Google search arguments you can make use of, for searching the SAS support http://support.sas.com/ website, which has much (free!) information including SAS-hosted DOC and supplemental technical / conference papers on this type of learning - topic.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic / post:

intro data step programming site:sas.com

intro input function site:sas.com

intro anyalpha anydigit function site:sas.com
Respected Advisor
Posts: 3,799

Re: Converting Numeric Column into Character Column by a macro

Are you sure you really want to do this? Do you really want to use a macro?

[pre]
data class / view=class;
set sashelp.class;
length dummy $32; /*or $256*/
retain dummy ' ';
run;
proc transpose data=class out=class2(where=(_name_ ne 'dummy'));
var dummy _numeric_;
by name sex;
run;
proc transpose data=class2 out=class0(drop=_Smiley Happy;
var col1;
by name sex;
run;
proc contents varnum;
run;
proc print;
run;
[/pre]
Valued Guide
Posts: 634

Re: Converting Numeric Column into Character Column by a macro

If you really do want to use a more complicated macro solution take a look at Example A1.6.4 in Carpenter's Complete Guide to the SAS Macro Language, Second Edition
Ask a Question
Discussion stats
  • 3 replies
  • 184 views
  • 0 likes
  • 4 in conversation