BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
BIN_AT09S=input(put(AT09S,AT09Sf.),$30.);

 

what exactly is happening here? AT09S is return in some format AT09Sf. and then put in another format $30.?

 

is this a typical use of SAS code?

10 REPLIES 10
s_lassen
Meteorite | Level 14

Not very typical. The PUT function converts the numeric variable to text, and then the input function reads that text, and returns exactly the same text (or, if the output from the custom format is longer, the first 30 characters). So the only thing the INPUT function does is to make the output variable length 30.

 

The code would be a lot easier to read if it looked like this:

length BIN_AT09S $30;
BIN_AT09S=put(AT09S,AT09Sf.);

Here is is very clear what we are doing: define a text variable of length 30, and then put something into it.

HeatherNewton
Quartz | Level 8
AT09S,AT09Sf.

 

are the above some standard format? what do they mean?

 

s_lassen
Meteorite | Level 14

Does not look standard to me, no mention of it in SAS documentation. If the format is user written (and available to your program), it should be in one of the libraries/catalogs mentioned in the FMTSEARCH option in the program or the calling environment. For instance, if option FMTSEARCH = (work library xdata.custom), you would expect the format to be in one of the catalogs WORK.FORMATS, LIBRARY.FORMATS or XDATA.CUSTOM.

 

You may then be able to find it, take a look at the CNTLOUT and FMTLIB options for PROC FORMAT.

 

Hope this helps.

Kurt_Bremser
Super User

@HeatherNewton wrote:
AT09S,AT09Sf.

 

are the above some standard format? what do they mean?

 


These are the two arguments to the PUT function in the code you posted earlier. The first argument is an expression (either numeric or character), the second is the format which the function will use to convert the value resulting from the expression. The format type must match the expression type.

In your case, the expression consists of a single variable, which needs to be numeric (as the format is a numeric format).

The format is no "standard" SAS format, so you need to see where this format is defined.

Run this:

data test;
set sashelp.vformat;
where upcase(fmtname) ="AT09SF";
run;

to see if (and when, where) the format is available in your current SAS session.

HeatherNewton
Quartz | Level 8
I found more code

proc format;
value AT09Sf
0-<4="01. 0-<4"
4-<8 = "02. 4-<8"
8-<12 ="03. 8-<12"
high ="04. >=12"
other="Other"

for the following code, does it mean "01. 0-<4" is returned instead of "0-<4" if AT09Sf meets certain condition ?

HeatherNewton
Quartz | Level 8
proc format
invalue 
AT09Ss
"01. 0-<4"=65
"02. 4-< 8"=54
...

what about proc format invalus statements? 65 is output instead of "01. 0-<4":

Kurt_Bremser
Super User

@HeatherNewton wrote:
proc format
invalue 
AT09Ss
"01. 0-<4"=65
"02. 4-< 8"=54
...

what about proc format invalus statements? 65 is output instead of "01. 0-<4":


Since this code defines an informat, there is no output, but input; the format can only be used in INPUT statements and INPUT functions. The result of the INPUT function will then be numeric (as the informat is numeric).

Kurt_Bremser
Super User

Please consult the documentation: VALUE Statement and Specifying Values or Ranges 

 

Your format converts numeric values that fall within ranges to character values.

 

To really work, your format should have a better range for 12 and above ("04"):

proc format;
value AT09Sf
  0-<4="01. 0-<4"
  4-<8 = "02. 4-<8"
  8-<12 ="03. 8-<12"
  12-high ="04. >=12"
  other="Other"
;
run;
HeatherNewton
Quartz | Level 8

so for command unput(put(AT09S,AT09Sf.),$30),

eg if AT09S=0, then what would be the value of AT09Sf?

At09sf returns as "01. /0-<4"?

Kurt_Bremser
Super User

@HeatherNewton wrote:

so for command unput(put(AT09S,AT09Sf.),$30),

eg if AT09S=0, then what would be the value of AT09Sf?

At09sf returns as "01. /0-<4"?


Nothing (except a syntax ERROR), as there is no UNPUT function in SAS.

 

SCNR

 

input(put(AT09S,AT09Sf.),$30.)

means that the result of the numeric-to-character conversion is returned as a 30-character string

Using

var = input(put(AT09S,AT09Sf.),$30.)

in a simple assignment would be redundant, as the result of the PUT function is already of type character.

To get a resulting variable of length 30, do this:

length var $30;
var = put(AT09S,AT09Sf.);

But if this part of a statement can be considered good coding practice can only be determined once the bigger picture is known, which means we need to see the whole context where this is used (defined length of target, whole assignment statement, intended later use).

 

All your questions tell us that you are confronted with VERY badly written code (which is in itself an overwhelming task for a beginner); in your own interest (and in the interest of everybody coming after you) you should rewrite all this stuff in proper, readable, maintainable, efficiently working manner. Every minute you spend now will save hours/days in the future.

As a hint for your superiors: it might make great sense to hire a consultant who digs through several codes with you and shows you how they should be written and how you make them more efficient. A few days at the hands of a seasoned SAS pro will teach you a LOT about good SAS coding, and also about how you use the tools provided (especially the excellent - albeit sometimes hard to find - SAS documentation. Employing Maxim 6 - Google Is Your Friend - will often help. A simple search for "sas invalue" returns the link I gave you in the previous post as a first hit).

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 1578 views
  • 2 likes
  • 3 in conversation