Hi there,
I have a over 50 numeric variables and and I like to apply FORMAT to all at once (eg: Best32.). Is there a way to apply format to all numeric values without writing all variables in the script?
Thanks,
Mike
/* data you have */
data WORK.test;
array allNumeric[50] (1:50);
array allCaracter[50] $ (50*"X");
do i=1 to 10;
output;
end;
format _numeric_ roman.;
run;
title "Before";
proc print data=work.test;
run;
/* code to do */
proc datasets lib=WORK noprint;
modify test;
format _numeric_ best32.;
run;
quit;
title "After";
proc print data=work.test;
run;
@mlogan wrote:
Hi there,
I have a over 50 numeric variables and and I like to apply FORMAT to all at once (eg: Best32.). Is there a way to apply format to all numeric values without writing all variables in the script?
Thanks,
Mike
Proc datasets will do that. The first data step copies a set you should have available to test code with and not mess up the original.
data work.cars; set sashelp.cars; run; proc datasets library=work; modify cars; format _numeric_ best32.; run; quit; /* examine the results*/ proc contents data=work.cars; run;
Proc datasets does this by modifying the header information in the data set and does not have to read the actual observations. So the time to execute doesn't vary much if the data set has 10 observations or 10 billion.
Thanks Ballardw. Your solution works for me, but I have a date field and _numeric_ function is converting the date field to numeric as well. Any solution to that?
Thanks.
@mlogan wrote:
Thanks Ballardw. Your solution works for me, but I have a date field and _numeric_ function is converting the date field to numeric as well. Any solution to that?
Thanks.
Provide a separate entry for your other variable(s) after the _numeric_. Sort of a "last one wins". An example using a data set with a date variable you should have.
data work.stocks; set sashelp.stocks (obs=50); run; proc datasets library=work; modify stocks; format _numeric_ best32. date date9. ; run; quit;
This is actually not uncommon with many statements in procedures. If you provide two different format statements for the same variable the last one is typically the result. If you use two Xaxis statements in Proc Sgplot the last is the one in effect, not the combined elements of two statements.
Maybe not all, but generally the way to bet.
If the variables have an order you can list them to include the numeric variables only in the list excluding date. If date is in the middle you could split it into two lists.
Or use the order approach.
data heart;
set sashelp.heart;
format AgeCHDdiag - NUMERIC - systolic 8.4;
run;
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.