BookmarkSubscribeRSS Feed
craig159753
Quartz | Level 8

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! 

6 REPLIES 6
data_null__
Jade | Level 19

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

craig159753
Quartz | Level 8

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 😕

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

craig159753
Quartz | Level 8

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

MikeZdeb
Rhodochrosite | Level 12

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 6 replies
  • 3850 views
  • 0 likes
  • 4 in conversation