BookmarkSubscribeRSS Feed
JackZ295
Pyrite | Level 9

Is there an efficient way to format over a hundred variables in SAS? For example, for every variable, I am currently applying an fname for it. Ex: 

 

format sex sexf. occ occf. ptcare ptcaref. shift shiftf.
specialty specialtyf. dept deptf. loc locf. **bleep** prickf.
sharps sharpsf. sharpsyr sharpsyrf. injrecap injrecapf.

 

Also, because I have a long list of variables, can I use multiple rows to mention the variables and their fnames? Thanks! 

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

Seems like the names of the formats in this example are always the name of the relevant variable with an 'f' on the end. Does that logic hold?

JackZ295
Pyrite | Level 9

Thanks! Yes, those are the formats I have been applying. My question is if there is a more efficient way to apply formats if I have a large number of variables. I have about 131 variables to format. Is this the only way to format my variables, by formatting them one by one? Thanks! 

PeterClemmensen
Tourmaline | Level 20

Is there some logic that tells you what those 131 variables are? Are those 131 variables all of the variables from the data set?

JackZ295
Pyrite | Level 9

Yes, all 131 variables are from the same dataset. 

PeterClemmensen
Tourmaline | Level 20

Yes, but are those all the variables from that data set? Does the data set contain more than those 131 variables?

JackZ295
Pyrite | Level 9

Yes, those are all of the variables in the dataset. 

Tom
Super User Tom
Super User

@JackZ295 wrote:

Is there an efficient way to format over a hundred variables in SAS? For example, for every variable, I am currently applying an fname for it. Ex: 

 

format sex sexf. occ occf. ptcare ptcaref. shift shiftf.
specialty specialtyf. dept deptf. loc locf. **bleep** prickf.
sharps sharpsf. sharpsyr sharpsyrf. injrecap injrecapf.

 

Also, because I have a long list of variables, can I use multiple rows to mention the variables and their fnames? Thanks! 


You can definitely use multiple lines for a single SAS statement.  The SAS parser in general ignores end of lines and treats them like any other white space.

format
  sex sexf.
  occ occf.
  ptcare ptcaref.
  shift shiftf.
  specialty specialtyf.
  dept deptf.
  loc locf.
....
;

If you want help generating that statement you could potentially read the metadata on an existing dataset.

proc contents data=have noprint out=contents; run;
proc sql noprint ;
  select catx('=',name,cats(name,'f.'))
    into :fmtlist separated by ' '
  from contents
  ;
quit;
proc datasets nolist lib=work ;
  modify have ;
  format &fmtlist;
run;
quit;

But it would probably be better to have the metadata already in a dataset somewhere.  That would also allow you to not make multiple formats that use the same set of code/decode pairs.  For example is normal to have multiple variables that all use the response pattern, like YES/NO or a scale from 1 to 10.  So instead of making multiple format just make one and attach to all variables that need it.  So your metadata might look like:

data datasets ;
  infile cards dsd dlm='|' truncover ;
  input memname :$32. memlabel :$256. ;
cards;
ae|Adverse Events
demo|Patient Demographics
;

data variables ;
  infile cards dsd dlm='|' truncover ;
  input memname :$32. name :$32. type :$4. length format :$41. informat :$41. label :$256. ;
cards;
ae|id|char|10|||Patient Identifier
ae|aedate|num|8|date9.|date9.|Date of Adverse Event
ae|aename|char|40|||Adverse Event description
ae|aeser|num|8|yesno.||Serious AE?
demo|id|char|10|||Patient Identifier
demo|age|num|8|||Patient Age at enrollment
demo|enrolldt|num|8|date9.|date9.|Date of enrollment
demo|active|num|8|yesno.||Active?
;

Which you could then use to generate a program. Perhaps something like this:

filename code temp ;
data _null_;
  file code ;
  merge datasets variables ;
  by memname ;
  if first.memname then put 'data ' memname '(label=' memlabel :$quote. ');' ;
  put '  attrib ' name 'length=' @ ;
  if type='char' then put '$' @ ;
  put length @ ;
  if not missing(format) then put format= @;
  if not missing(informat) then put informat= @;
  if not missing(label) then put label = $quote. @ ;
  put ';' ;
  if last.memname then put '  stop;' / 'run;' / ;
run;

to get a program like this:

data ae (label="Adverse Events" );
  attrib id length=$10 label="Patient Identifier" ;
  attrib aedate length=8 format=date9. informat=date9. label="Date of Adverse Event" ;
  attrib aename length=$40 label="Adverse Event description" ;
  attrib aeser length=8 format=yesno. label="Serious AE?" ;
  stop;
run;

data demo (label="Patient Demographics" );
  attrib id length=$10 label="Patient Identifier" ;
  attrib age length=8 label="Patient Age at enrollment" ;
  attrib enrolldt length=8 format=date9. informat=date9. label="Date of enrollment" ;
  attrib active length=8 format=yesno. label="Active?" ;
  stop;
run;

 

Astounding
PROC Star

While @Tom gave you a few good approaches, you also have to consider whether this might be a bad idea.  If you assign the formats as part of a DATA step, those formats are permanently associated with the variables.  As a result, any time you want to use the data, all 131 formats must exist.  Is that an acceptable situation?

JackZ295
Pyrite | Level 9

I'm confused by what you mean when you say that any time I want to use the data, all 131 formats have to exist. Can you elaborate on your response? Thanks! 

Astounding
PROC Star

Let's say you have this situation where the format is permanently attached to a variable:

 

data with_format;

set have;

format sex sexf.;

run;

 

That's fine as long as the format exists.  But if you later use this:

 

data want;

set with_format;

run;

 

Now the format must exist or else you will get an error message.  You can program around that.  This statement requests that there is no error message when a required format cannot be found:

 

options nofmterr;

 

But once you permanently associate a format with a variable, you need that format to exist when you use the data set.

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 25. 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
  • 11 replies
  • 1357 views
  • 4 likes
  • 4 in conversation