DATA Step, Macro, Functions and more

How to use 'put' to change data format into charater?

Reply
Super Contributor
Posts: 297

How to use 'put' to change data format into charater?

Hello:

 

I have a SAS dataset with 30 variables with date format and 120 variables with numeric formate.  I need to change all of them into charterics.   Is there a way I could refer them all in once instead of typing them all?   This is not one time use.  I am expecting I will have more data coming from different sources with different counts of date and numeric variables.  I hope I don't need to type them again and again.  Thanks.

 

Super User
Posts: 17,829

Re: How to use 'put' to change data format into charater?

Create two arrays. Use the _character_ and _numeric_ variable list shorthand. 

 

Super User
Posts: 10,500

Re: How to use 'put' to change data format into charater?

Depending on what you actually attempt and are going to use the data for be aware that you may need to consider leading blanks in the resulting variables.

 

 

Super User
Super User
Posts: 7,401

Re: How to use 'put' to change data format into charater?

Read the instructions at the bottom of the post a question.  Post example test data in the form of a datastep, and what you want the output to look like.  From the little you post I would guess by date variables you have numeric date variables.  Is there a simple way of identifying these from the name?  If so you can use @Reeza's suggestion and simply:

data want;
  set have;
  array nums _numeric_;
  array char_res{150} $100;
  do i =1 to 150;
    call vname(nums{i},name);
    if substr(name,1,4)=date then char_res{i}=put(nums{i},date9.);
    else put(nums{i},best.);
  end;
run;
Super User
Super User
Posts: 6,500

Re: How to use 'put' to change data format into charater?

[ Edited ]

Where is the list of variable names to convert coming from? Do you have them in a dataset? Or as a space delimited list of names in a macro variable?

Are there going to be any numeric variables left in the data set?  

 

It might just be easier to write the data to a text file and read it back in using the same variable names, but as character varaibles.

proc contents data=have noprint out=contents; run;
proc sql noprint ;
  select varnum,name 
    into :varlist,:varlist separated by ' ' 
    from contents
    order by 1
  ;
quit;
filename rawdata temp;
data _null_;
  set have ;
  file rawdata dsd lrecl=1000000 ;
  put &varlist ;
run;
data want ;
  length &varlist $20 ;
  infile rawdata dsd truncover lrecl=1000000;
  input &varlist ;
run;

 

Super Contributor
Posts: 297

Re: How to use 'put' to change data format into charater?

Thanks for the thoughtful explaination.

Super User
Posts: 9,681

Re: How to use 'put' to change data format into charater?

Make a macro.    


data class;
 set sashelp.class;
run;

data _null_;
 set sashelp.vcolumn(
keep=libname memname type name
where=(libname='WORK' and memname='CLASS' and type='num')) end=last;
if _n_=1 then call execute('data want; set class;');
call execute(catt('char_',name,'=vvalue(',name,');'));
if last then call execute('run;');
run;      
Ask a Question
Discussion stats
  • 6 replies
  • 144 views
  • 3 likes
  • 6 in conversation