BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mgrasmussen
Quartz | Level 8

Dear SAS experts

 

I have in my dataset a value label (value label: a term I know from working with Stata for many years) created using proc format; value specification. It is intended for a variable which contains thousands of data points and therefore the value label includes specification of MANY values. However, I would very much like to add just one more value label (a label for .). 

 

Is it possible to simply modify the existing value label such that it includes one more value label, while maintaining the existing value labels? 

 

I am working in a system where I can simply upload the format and thereby apply the value label directly, i.e. I don't actually have to specify the value label using proc format; value.. This saves me MANY lines of code. I can upload the full code into my program file and thereby simply add the additional value label (i.e. add an additional line of code), but I would much rather simply modify the existing format and - if possible - save many lines of code.

 

Thank you

 

Best regards

 

Martin

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What do you want display missing values as?  If it is just a single letter try just changing the MISSING system option.

 

Why not define a new format that references the old format and overrides the decode used for missing values.

proc format ;
value newfmt
  . = 'New decode'
  other = [oldfmt.]
;
run;

And then use the new format with your variable(s).

proc freq data=have;
  format varname newfmt.;
  tables varname;
run;

Otherwise please explain more clearly what you are doing.

 

If you have SAS code that defined the format changing the way it displays missing values should be a simple one line change to the code.

If you have a SAS dataset you are using with CNTLIN= option of PROC FORMAT to define the format then a simple one line change to the dataset is all you will need.  Make sure you understand how to use the HLO variable in the control dataset.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

To modify a format to add a condition when the original data is missing, you have to change the VALUE statement in PROC FORMAT that created the format. Then you have to run this changed PROC FORMAT.

--
Paige Miller
Astounding
PROC Star

The programming to do this is not simple, but is relatively short.

 

If you are using a permanently saved format, you need to now the name of both the format itself (easy) and the format catalog holding the format (not as easy).  Then it would take three steps:

 

  1. Download the contents of the format into a SAS data set.  This means using PROC FORMAT with the CNTLOUT= option.
  2. Modify that downloaded SAS data set (see code below).
  3. Re-generate the format using the modified SAS data set.  This means using PROC FORMAT with the CNTLIN= option.

Sample code to modify the SAS data set that was downloaded from the format:

data modified_version;
   set downloaded_format end=done;
   output;
   if done;
   start = /* new value being defined in the format */;
   end = /* same new value being defined in the format */;
   label = /* label you want to assign to that new value */;
   output;
run;

Take a look and see if this seems like a viable approach for you.  THere are definitely a few complex details, but perhaps only a dozen lines of programming.

BrunoMueller
SAS Super FREQ

You can always use %include to copy the code of an existing SAS program into your current code.

 

So you can keep the definition of a format in a separate file and use this wherever you need it.

 

If you really want to add another "line" to an existing format checkout the code sample below. This will overwrite the existing format.

 

/*
 * original format
 */
proc format;
  value myfmt
    11 = "eleven"
    12 = "twelve"
    13 = "thirteen"
  ;
run;

/*
 * use it
 */
proc print data=sashelp.class;
  format age myfmt.;
run;

/*
 * write out definition
 */
proc format cntlout=fmtdef;
  select myfmt;
run;

/*
 * insert additional "line"
 */
proc sql;
  insert into fmtdef
    set
      fmtname = "myfmt"
      , type = "n"
      , start = "14"
      , end = "14"
      , label = "fourteen"
  ;
quit;

/*
 * recreate it
 */
proc format cntlin=fmtdef;
run;

/*
 * check it out
 */
proc print data=sashelp.class;
  format age myfmt.;
run;
Tom
Super User Tom
Super User

What do you want display missing values as?  If it is just a single letter try just changing the MISSING system option.

 

Why not define a new format that references the old format and overrides the decode used for missing values.

proc format ;
value newfmt
  . = 'New decode'
  other = [oldfmt.]
;
run;

And then use the new format with your variable(s).

proc freq data=have;
  format varname newfmt.;
  tables varname;
run;

Otherwise please explain more clearly what you are doing.

 

If you have SAS code that defined the format changing the way it displays missing values should be a simple one line change to the code.

If you have a SAS dataset you are using with CNTLIN= option of PROC FORMAT to define the format then a simple one line change to the dataset is all you will need.  Make sure you understand how to use the HLO variable in the control dataset.

sas-innovate-2024.png

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.

 

Register now!

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
  • 4 replies
  • 861 views
  • 4 likes
  • 5 in conversation