sas tips - the humble put statement, phil mason - sas

35
Copyright © 2015, SAS Institute Inc. All rights reserved. SAS Tips The humble PUT statement

Upload: others

Post on 23-Nov-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Copyr igh t © 2015 , SAS Ins t i tu te Inc . A l l r i gh ts reserved .

SAS Tips

The humble PUT statement

Writes values out separated by a space

data _null_ ; x=1 ; y=2 ; z=3 ; Put x y z ; run ;

1 2 3

Write values followed by a tab, useful for making tab-separated files

data _null_ ; Put 'Cost' '09'x '£20' ; run ;

Cost £20

Write 132 underscoresdata _null_ ; Put 132*'_' ; run ;

_________________________________________________________________________________________________________________________

Write a value starting at a specific line and column

data _null_ ; cost=123.45 ; Put #3 @44 cost ; run ;

123.45

Write value into specific columns, only writing parts of value that fit

data _null_ ; var='abcdefghijklm' ; Put var 1-5 ; run ;

abcde

Write a formatted value, in GBP or EUR

data _null_ ; cost1=123.45 ; cost2=67.89 ; Put cost1 nlmnlgbp. cost2 nlmnleur. ; run ;

£123.45 €67.89

Write a list of variables using a list of formats, with separators

data _null_ ; a=3 ; b='dog' ; Put (a b) (1. ',' $3.) ; run ;

3,dog

Write current input buffer, as read by last input statement

data _null_ ; input ; Put _infile_ ; cards ; test this input file ;; run ;

test this input file

Write the values of all variables, including _error_ & _n_

data _null_ ; set sashelp.class(obs=1) ; Put _all_ ; run ;

Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 _ERROR_=0 _N_=1

Write values to a line, and then some more to the same line

data _null_ ; a=1 ; b=2 ; c=3 ; Put a b c @ ; put 'this will be on the end of the line' ; run ;

1 2 3 this will be on the end of the line

Write value at a specific position on line

data _null_ ; name='phil mason' ; Put '|' @10 name ; *Write value of name at column 10; run ;

| phil mason

Write to a column specified by a variable

data _null_ ; pos=7 ; name='Mike Davis' ; Put '|' @pos name ; run ;

| Mike Davis

Write value at column specified as a variable times a factor

data _null_ ; pos=5 ; name='Jim Goodnight' ; Put '|' @(3*pos) name ; run ;

| Jim Goodnight

Write value with a number of spaces between them

data _null_ ; a='one' ; b='two' ; Put a +3 b ; run ;

one two

Write values with a number of spaces between them specified by variabledata _null_ ; a='one' ; gap=20 ; b='two' ; Put a +gap b ; run ;

one two

Write values separated by spaces determined by an expression

data _null_ ; a='one' ; gap=20 ; b='two' ; Put a +(2*gap) b ; run ;

one two

Write a value at a specified linedata _null_ ; text='line 2' ; Put #2 text ; run ;

line 2

Write value to a line specified in a variable

data _null_ ; line=3 ; text='line 3' ; Put #line text ; run ;

line 3

Write value to a line specified by an expression

data _null_ ; line=2 ; text='2 * 2' ; Put #(line*2) text ; run ;

2 * 2

Write variables and go to a new linedata _null_ ; line1='i am line 1' ; line2='i am line 2' ; Put line1 / line2 ; run ;

i am line 1 i am line 2

Overprint will print over existing text in a file (no effect on screen)

data _null_ ; title='my report' ; Put @1 title overprint @1 '________' ; * underline ; run ;

my report ________

Print an entirely blank pagedata _null_ ; put 'something' ; Put _blankpage_ ; * writes carriage control chars ; put 'next thing' ; run ;

something next thing

Go to a new pagedata _null_ ; put 'something' ; Put _page_ ; * next page ; put 'next thing' ; run ;

something next thing

Write values prefixed by variable names

data _null_ ; name='phil' ; phone='200635' ; Put name= phone= ; run ;

name=phil phone=200635

Write values of an arraydata _null_ ; set sashelp.prdsale(obs=1) ; array my_big_array(*) _character_ ; Put my_big_array(*) ; run ;

CANADA EAST EDUCATION FURNITURE SOFA

data _null_ ; set sashelp.prdsale(obs=1) ; array my_big_array(*) _character_ ; Put my_big_array(*)= ; run ;

COUNTRY=CANADA REGION=EAST DIVISION=EDUCATION PRODTYPE=FURNITURE PRODUCT=SOFA

Write a variable using a format & justify value within field width

data _null_ ; x=1 ; Put '-' x 5. -c '-' ; * -l, -c, -r ; run ;

- 1 -

Write formatted values separated by some text

data _null_ ; x=1 ; y=2 ; z=3 ; Put (x y z) (1. ', ') ; run ;

1, 2, 3

Write character values with no separating space

data _null_ ; x=1 ; y=2 ; Put x +(-1) y ; run ;

12

Writing values with a format, but removing surrounding spaces

data _null_ ; first='Jim' ; last='Goodnight' ; Put first : $32. last : $32. 'founded SAS.' ; run ;

Jim Goodnight founded SAS.

Write value to a file using stripped formatted quoted value

data _null_ ; file log dsd ; cost=123 ; item='Chair' ; Put item ~ $10. cost ~ dollar12. ; run ;

"Chair","$123"

Use PUTLOG to write to log when writing to another location

filename temp temp ; data _null_ ; file temp ; put 'something' ; putlog 'We wrote something to temp' ; run ;

We wrote something to temp

Write to ODS destinationsdata _null_ ; file print ods=( variables=( name age(format=z3.) sex(label='Gender'))) ; set sashelp.class ; put _ods_ / @1 height @2 weight ; run ;

Copyr igh t © 2015 , SAS Ins t i tu te Inc . A l l r i gh ts reserved .

www.SAS.com

Philip Mason e-mail - [email protected]