- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
/* 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;
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content