BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
xxformat_com
Barite | Level 11

Hi,

 

The SAS Online Doc suggests that the difference between the put function using a character format and the input function using a character informat would be the way trailing blanks would be managed. See example 4

 

From earlier experiences, I can remeber using -l to left align the result of character values created using the put function and have trailing blank then.

 

However when testing some code I cannot reproduce such case.

 

data test;
    length a $8;
    a='123.15'; output;
    a='12345';  output;
    a='-123';   output;
run;

data _null_;
    set test;
    a1=input(a,$20.);
    a2=put(a,$20.);

    put 'Start' a1 'End';
    put 'Start'  a2 'End';
run;

put_input.JPG

 

1 ACCEPTED SOLUTION

Accepted Solutions
xxformat_com
Barite | Level 11

Thank you @ballardw Your feedback helped me creating the example I was looking for.

 

Here I use the -r you mentionned to move trailing blanks at the front.

This way we can see that trailing blanks are added in the put function but not in the input function.

 

proc format;
    value   $sex  'M'  = 'Male'
                  'F'  = 'Female';
    invalue $sex  'M'  = 'Male'
                  'F'  = 'Female';
run;

data class;
    set sashelp.class;
    new_put  =put  (sex,$sex.-r);
    new_input=input(sex,$sex.-r);
run;

ods listing file="&xxtraining./class.lst";
proc print data=class (obs=8) noobs;
   var sex new:;
run;
ods listing close;

trailing_blanks.JPG

 

As a whole, I'm looking for any benefit in using input function (char informat) over put function (char format).

So far, trailing blanks is the only one I could identify.

 

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

You use the PUT Statement in List mode. From the documentation (see the link):

Using List Output

With list output, you list the names of the variables whose values you want written, or you specify a character string in quotation marks. The PUT statement writes a variable value, inserts a single blank, and then writes the next value. Missing values for numeric variables are written as a single period. Character values are left-aligned in the field; leading and trailing blanks are removed. To include blanks (in addition to the blank inserted after each value), use formatted or column output instead of list output.

(emphasis by me)

Kurt_Bremser
Super User

PS your example which you linked to is from the documentation of the SAS Component Language, which is different in many ways from the data step language.

SCL is something you will rarely (most likely never) need. Always start your inquiry into SAS programming documentation here (and make sure Google searches land you within this area):

Programming Documentation for SAS® 9.4 and SAS® Viya® 3.5 

ballardw
Super User

@xxformat_com wrote:

 

From earlier experiences, I can remeber using -l to left align the result of character values created using the put function and have trailing blank then.

 


That -L in a put is typically used with NUMERIC formats because the default behavior with numeric formats is right justification and put with  something like x=Put(4, f8.) will result in a value for x with 7 leading spaces before the 4. Character formats by default left justify so if you think you need a leading space using Put with a character value then you use the -R to right justify.

Examples with literal values so no one has to guess what the actual values or properties of not documented variables might be:

data junk;
   x=put(4,f8.);
   x2=put(4,f8. -L);
   y=put('A', $10. -r);
   y2=put('A', $10.);
run;

It might help a lot to show what you are actually attempting to create. Writing to the LOG also has slightly different behavior than creating variables because the lines in the LOG can contain many values from a single PUT. Typically you need to provide some additional information such as column position adjustments such as in the following to ensure one "blank" before the value of a1 and 5 spaces before End.

 put 'Start' +1 a1 +5 'End';

You might also want to test some variants like this:

 put 'Start: ' a1 '    End';

 

xxformat_com
Barite | Level 11

Thank you @ballardw Your feedback helped me creating the example I was looking for.

 

Here I use the -r you mentionned to move trailing blanks at the front.

This way we can see that trailing blanks are added in the put function but not in the input function.

 

proc format;
    value   $sex  'M'  = 'Male'
                  'F'  = 'Female';
    invalue $sex  'M'  = 'Male'
                  'F'  = 'Female';
run;

data class;
    set sashelp.class;
    new_put  =put  (sex,$sex.-r);
    new_input=input(sex,$sex.-r);
run;

ods listing file="&xxtraining./class.lst";
proc print data=class (obs=8) noobs;
   var sex new:;
run;
ods listing close;

trailing_blanks.JPG

 

As a whole, I'm looking for any benefit in using input function (char informat) over put function (char format).

So far, trailing blanks is the only one I could identify.

 

Tom
Super User Tom
Super User

I am surprised that SAS would even allow those -R modifiers on an INFORMAT.

What would that even mean?

 

If you don't want INPUT statement or INPUT() function to remove leading spaces when reading a character variable from text use the $CHAR informat. 

 

With INPUT statement you can also use the $VARYING informat, but I doubt that would work with INPUT() function as there is no way to provide it the extra length information it needs.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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
  • 5 replies
  • 491 views
  • 1 like
  • 4 in conversation