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

Hi, this is probably straighforward but...

I'm using the following code within a datastep:

x =  PUTN(VARIABLE1, FORMATTABLE);

Basically, variable one is age and formattable is a tables that has ranges (start and end values) and if the age is within the range it returns a letter (the Label)

The dataset is huge and in some cases there won't be a match and i just want sas to skip over it or give it a value of blank. It is leaving it blank in these cases but I get this horrible message in the log 'Argument 2 to PUTN at line xx column xx is invalid' followed by a long list of the values for the row.

Is there a cleaner approach to this in sas. A bit like in excel where you use iferror(var1, "")??

Many thanks

Matthew

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

I think there is easy fix for you:

1. In your format table, add one row to take care of those non-matched records:

Fmtname = LICAGENONMATCH

Start = 0 /*VALUE IS NOT RELEVANT*/

End 5  /*VALUE IS NOT RELEVANT*/

Label = /*BLANK HERE*/

2, Change your  statement as the following;

LicenseAge = Putn(licenseage, Cats("LICAGE", coalescec(product, 'NONMATCH'))); /*WHEN PRODUCT IS MISSING, YOU WILL HAVE FORMAT  LICAGENONMATCH IN PLAY, AND  LICAGENONMATCH HAS ALREADY BEEN DEFINED.*/

Good luck and sorry for the caps,

Haikuo

View solution in original post

9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

PUTN() is not the function for what you are attempting to do.  PUTN() is used to put a numeric variable into a specific numeric format at run-time.  What I believe you are trying to do is to set a format on the numeric.  E.g.

proc format;

     value myfmt

     1-10 = "A"

     11-20 = "B";

run;

data have;

  attrib myval format=best.;

  attrib result format=$20.;

  myval=1;

  result=put(myval,myfmt.);

  output;

  myval=15;

  result=put(myval,myfmt.);

  output;

run;

brophymj
Quartz | Level 8

The PUTN function works perfectly for this as long as there is a corresponding value in the table. If the age is 10 and the start value in the table formatable is 9 and the end value is 11 and the label for this range is A, the above function returns A and it has worked for me. It's when there is no match I want a clean way of avoiding error messages.

Reeza
Super User

Pre calculate the value for your format, and if it's missing have a backup format, either in your format table.


proc format;

value missing_fmt

missing = "NA";

run;

if missing(format_wanted) then format_wanted="missing_fmt.";

new_var=putn(var, format_wanted);

brophymj
Quartz | Level 8

HI Reeza thanks for your help. I've tried that but I'm still get errors. I've attached the exact code i used for the datastep.

Thank you

Haikuo
Onyx | Level 15

We would need more details.  From the error message It seems to me that it not the  VARIABLE1 that is the culprit, as if it is, the putn will just throw a missing value. Please check out the following example and reveal more necessary details about FORMATTABLE:

proc format;

   value writfmt 1='date9.'

                 2='wrong format';

run;

quit;

data dates;

   input number key;

   datefmt=put(key,writfmt.);

   date=putn(number,datefmt);

   datalines;

15756 1

14552 2

;

1 proc format;

2 value writfmt 1='date9.'

3 2='wrong format';

NOTE: Format WRITFMT has been output.

4 run;

NOTE: PROCEDURE FORMAT used (Total process time):

  real time 0.09 seconds

  cpu time 0.03 seconds

5 quit;

6 data dates;

7 input number key;

8 datefmt=put(key,writfmt.);

9 date=putn(number,datefmt);

10 datalines;

NOTE: Argument 2 to function PUTN at line 9 column 9 is invalid.

RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+-

12 14552 2

number=14552 key=2 datefmt=wrong format date= _ERROR_=1 _N_=2

NOTE: The data set WORK.DATES has 2 observations and 4 variables.

NOTE: DATA statement used (Total process time):

  real time 0.13 seconds

  cpu time 0.14 seconds

13 ;

Regards,

Haikuo

brophymj
Quartz | Level 8

Hi Hai.kuo

Apologies for not giving more detail. A bit of background...

I created a format table before this code using the 'proc format cntlin = table1 fmtlib; run;

the table was a licence age table which contained the table name, ages ranged, and a banding corresponding to the range. So the first row of the table was LICAGEAAA  0 5 A.

So, the table had 4 fields:

Fmtname = LICAGEAAA

Start = 0

End 5

Label = A 

The format table consists of many rows as this relates to products (product AAA in example above) but the licence age ranges vary by product.

Later on in the code I need to reference this table so I can attache on the label to my dataset. So, if the licence age in one of the rows of my dataset is 4, i want to attach on the label A. Reeza kinfly helped me with this last week and it worked.

The code I use to call the label from this table is

LicenseAge = Putn(licenseage, Catt("LICAGE",product));

Licenseage and product are two fields in my dataset - Reeza helped me out with the Catt function.

My query here is that if the CATT("Licage",Product) for a particular row has no match in the table, It leaves it blank but I get a formatting error in the log  which I don't like. It actually doesn't affect the caluclation but I would like to replace these missing values with "N/A" and have no errors in the log.

Does this make sense at all?

Haikuo
Onyx | Level 15

I think there is easy fix for you:

1. In your format table, add one row to take care of those non-matched records:

Fmtname = LICAGENONMATCH

Start = 0 /*VALUE IS NOT RELEVANT*/

End 5  /*VALUE IS NOT RELEVANT*/

Label = /*BLANK HERE*/

2, Change your  statement as the following;

LicenseAge = Putn(licenseage, Cats("LICAGE", coalescec(product, 'NONMATCH'))); /*WHEN PRODUCT IS MISSING, YOU WILL HAVE FORMAT  LICAGENONMATCH IN PLAY, AND  LICAGENONMATCH HAS ALREADY BEEN DEFINED.*/

Good luck and sorry for the caps,

Haikuo

jakarman
Barite | Level 11

SAS(R) 9.3 Functions and CALL Routines: Reference putn see example 2.
The putn is needing a string as second parameter that is a valid format. When it is not a valid one it will fail.
If you are using a variable for that it is up to you coding in a way that assure it is having a valid format.   Good coding practice.

---->-- ja karman --<-----
jakarman
Barite | Level 11

You are telling you have a table of licenseage varying in product. 
For every product you have defined a format.  The can be a format (eg dummyNA)  when applied will always give the value 'N/A' 
You could define a format that applied to the product will result in the associated format. When no format is applicable then that dummyNA format is the resulting value. The result/goal always having a valid value existing format.    

---->-- ja karman --<-----

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!

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
  • 9 replies
  • 1180 views
  • 6 likes
  • 5 in conversation