Help using Base SAS procedures

User Defined Formats not Doing Anything to Values

Reply
Contributor
Posts: 39

User Defined Formats not Doing Anything to Values

[ Edited ]

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! 

Respected Advisor
Posts: 3,777

Re: User Defined Formats not Doing Anything to Values

What is the name of the format catalog?  You may need to specify it.  fmtsearch=(libname.myfmts) perhaps.

Contributor
Posts: 39

Re: User Defined Formats not Doing Anything to Values

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 :/

Super User
Super User
Posts: 7,401

Re: User Defined Formats not Doing Anything to Values

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;

Contributor
Posts: 39

Re: User Defined Formats not Doing Anything to Values

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;

Super User
Super User
Posts: 7,401

Re: User Defined Formats not Doing Anything to Values

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;

Valued Guide
Posts: 765

Re: User Defined Formats not Doing Anything to Values

[ Edited ]

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

 

Ask a Question
Discussion stats
  • 6 replies
  • 567 views
  • 0 likes
  • 4 in conversation