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.
Create two arrays. Use the _character_ and _numeric_ variable list shorthand.
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.
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;
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;
Thanks for the thoughtful explaination.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.