I have created a custom numeric format below. However, the format is not applied to the data. I can't understand the reason. Can you please have a look?
Please note that the code was executed in SAS On Demand.
libname myformat '/home/u48733392/Chapter11';
proc format library=myformat;
value tiers 20000-49999 = 'Tier 1'
50000-99999 = 'Tier 2'
100000-250000 = 'Tier 3';
run;
data myData;
input First $ Salary;
datalines;
Stacey 20000
Mary 30000
George 52256
Gloria 99999.87
Kammy 156906
James 250000
;
proc print data=myData;
run;
proc print data=myData;
var First Salary;
format Salary tiers.;
run;
Below is the output of the code:
Works fine for me.
Gloria's falls out because the previous format ends at 99,999 and the next one starts at 100,000 leaving the decimal fraction unaccounted for.
You may want to define it as follows instead:
proc format;
value tiers 20000-<50000 = 'Tier 1'
50000-<100000 = 'Tier 2'
100000-250000 = 'Tier 3';
run;
EDIT: my mistake, you created the format in a library so you also need to tell SAS which library to use to find the format. @japelin has provided that code.
Then you can fix the other issue with the suggested code above.
Use fmtsearch first.
options fmtsearch=(myformat);
Thank you for the quick reply. The code worked. I am preparing for the SAS base programmer exam and fmtsearch option is not mentioned in the prep guide. Can you pleas elet me know what is the exact use of 'fmtsearch'? Why wouldn't code work without it?
Format is searched in work library and library library in default.
fmtsearch can add specific library to search.
If you want more detail, please read document.
@YasodaJayaweera wrote:
Thank you for the quick reply. The code worked. I am preparing for the SAS base programmer exam and fmtsearch option is not mentioned in the prep guide. Can you pleas elet me know what is the exact use of 'fmtsearch'? Why wouldn't code work without it?
FMTSEARCH is a SAS environment setting for where to search for Formats.
If you place a format in a library other than WORK or the SAS supplied locations then you need to tell SAS where to look to find your format.
Your code:
proc format library=myformat;
specified a different library. If you had not provided the Library option the default would be to place the format in the WORK library and nothing needed.
You may actually want to APPEND your library to the FMTSEARCH path such as
options append=(fmtsearch=myformat);
to add the library to the end of the list of locations SAS searches (or Insert to make it the first location to search).
SAS searches for a specified format in the locations in order of appearance. If you use "options fmtsearch=myformat". The Work library will no longer be searched.
You can see your current setting for the option using:
proc options option=fmtsearch; run;
For example on the computer I am working with that shows this in the log:
FMTSEARCH=(WORK LIBRARY CENT) Specifies the order in which format catalogs are searched.
So when I use a format such as Somefmtname. SAS will first look in the Work library for the format, if not found then in LIBRARY (the SAS supplied formats) and then last in the CENT library.
Note that order can be important if you happen to have multiple formats of the same name.
I not-infrequently will want to temporarily modify a format to test something. So placing the test version in the WORK library will not disturb the one in the permanent library (Cent in this case) but will use the same name in the Work library as it is found there first.
In another environment I have 9 additional permanent libraries with formats for specific recurring projects. The FMTSEARCH option lets be specify all of them without having to try to rerun code or such.
Note that you can specify a Catalog name for the formats as well as the library. If you do such then you need to make sure the catalog is included in the FMTSEARCH list.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.