Hi,
I am currently working in SAS EG 6.1, and I am trying to convert character variables into their formatted counterparts using the put function. My program runs fine, no errors/warnings but the values do not change like I expect them too.
An example of the format I have
proc format
value $noyes
"1" = "Yes"
"2" = "No";
run;
Now this is one format which is stored in my permanent format catalogue, and a define a LIBNAME statement for it's location and I then use the following option to tell SAS EG where the catalogue is.
options fmtsearch = (LIBNAME);
Then in my program I go to derive a new variable, call it NEWVAR, like so
data out;
set in;
newvar = strip(put(oldvar, $noyes.));
run;
Now like I said no errors/warnings, the format is found but does nothing. The values in the OLDVAR are "1" and "2" only. OLDVAR is character. Oddly though, if i define $NOYES. in the same program, i.e. if i copy the above proc format above the data set and run, it will work perfectly. Any ideas??
I hope I explained this as well as i could, thanks in advance!
What is the name of the format catalog? You may need to specify it. fmtsearch=(libname.myfmts) perhaps.
Thanks for the reply, the catalogue is called FORMATS, I tired your suggestion no luck. I have also tried renaming it something totally different. Still no luck 😕
Not sure if this will be the reason, however in your proc format step you are missing a semicolon after the format. This code should work fine. If it still doesn't work when you fix this, then it probably isn't finding the format catalog and you will need to post your code for initialising the format catalog, and using it.
proc format;
value $noyes
"1" = "Yes"
"2" = "No";
run;
data want;
a="1"; b="1"; c=put(a,$noyes.); d=put(b,$noyes.);
run;
Thanks for the reply, the missing semi colon was a typo on here, the proc format code for my catalogue is like the following
proc format library = mylibrary cntlout = mylibrary.formats;
value $noyes
"1" = "Yes"
"2" = "No";
run;
I can see the catalogue is present and contains this $NOYES format by looking at the SASHLP.VCFORMAT and SASHELP.VCATALG data sets.
The actual code for using it was like in my first post
data output;
set input;
newvar = strip(put(oldvar, $noyes.));
run;
And the code runs without errors/warnings? The reason I ask is that I have run that code on 9.3 and it will not work. Mylibrary is more than 8 characters long. Maybe your EG is 9.4 I don't know.
I would check to see if there is a catalog called formats in the library you have created as this code worked fine for me, do note that if the format code is not in the same section of code to the current one then you need to do options fmtsearch=(mylib); and of course make sure the library mylib is assigned before that:
libname mylib "s:\temp\rob";
proc format library = mylib cntlout = mylib.formats;
value $noyes
"1" = "Yes"
"2" = "No";
run;
data output;
oldvar="1";
newvar = strip(put(oldvar, $noyes.));
output;
oldvar="2";
newvar = strip(put(oldvar, $noyes.));
output;
run;
Hi. Try this and see if it helps ...
* some data;
data x;
input x :$1. @@;
datalines;
1 2 1 2
;
* location of format library;
libname mylib 'z:\';
* create a format ;
* creates a CATALOG named FORMATS (default if no other name specified) in the library MYLIB;
* NO NEED for that CNTLOUT option (useless);
proc format library=mylib;
value $noyes
"1" = "Yes"
"2" = "No";
run;
* extra step for now ... check to see if the new format is in the catalog within the library;
proc catalog cat=mylib.formats;
contents;
run;
* tell SAS to include the new format catalog when looking for a formats;
* since you did not give the catalog a name, SAS infers that it chould look for a catalog named FORMATS in MYLIB;
options fmtsearch=(mylib);
* use the format (do not need STRIP function ... useless);
data xplus;
set x;
y = put(x,$noyes.);
run;
output from PROC CATALOG ...
Contents of Catalog MYLIB.FORMATS
# Name Type Create Date Modified Date Description
1 NOYES FORMATC 09/15/2015 11:46:53 09/15/2015 11:46:53
data set XPLUS ...
Obs x y
1 1 Yes
2 2 No
3 1 Yes
4 2 No
More info ... Yes, We Can... Save SAS® Formats
http://support.sas.com/resources/papers/proceedings12/048-2012.pdf
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!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.