BookmarkSubscribeRSS Feed
ybz12003
Rhodochrosite | Level 12

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.

 

6 REPLIES 6
Reeza
Super User

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

 

ballardw
Super User

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.

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Tom
Super User Tom
Super User

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;

 

ybz12003
Rhodochrosite | Level 12

Thanks for the thoughtful explaination.

Ksharp
Super User
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;      

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
  • 6 replies
  • 917 views
  • 3 likes
  • 6 in conversation