BookmarkSubscribeRSS Feed
mlogan
Lapis Lazuli | Level 10

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

6 REPLIES 6
yabwon
Onyx | Level 15
/* 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



ballardw
Super User

@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.

 

 

 

 

mlogan
Lapis Lazuli | Level 10

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.

ballardw
Super User

@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.

 

Reeza
Super User

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;

 

 

Reeza
Super User
Key idea, _numeric_ refers to all numeric variable and _character_ refers to all character variable. _all_ refers to all variables.

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 409 views
  • 6 likes
  • 4 in conversation