data perm_student;
input name$ age 2.;
cards;
afred 14
alice 13
barbara 13
carol 14
;
run;
data student;
set perm_student;
put name $15. @5 age 2.;
run;
it looks that 'put' does not work. The student dataset is only a simple copy of perm_student. could you help what is wrong?
Hi:
I am not sure why you are running this program. The PUT statement should be writing to the SAS Log, NOT to the SAS dataset. Did you look in the SAS Log after this program ran? The OUTPUT statement writes observations to the SAS dataset in the DATA statement. The PUT statement, without a FILE reference will write to the log in the same way as the PUTLOG statement. Consider these 2 DATA steps and 2 PROC PRINTS -- the differences should be obvious when you look at the PROC PRINTS and then look at the SAS log.:
data newfile;
set sashelp.class;
where age gt 14;
put @1 "** From the PUT statement: " @35 name= _n_= age=;
putlog "*** From PUTLOG statement: " name= _n_= age=;
run;
proc print data=newfile;
title 'SAS dataset work.newfile';
run;
data newfile2;
set sashelp.class;
where age gt 14;
file 'c:\temp\fromput.txt';
put @1 "** From the PUT statement: " @35 name= _n_= age=;
putlog "*** From PUTLOG statement: " name= _n_= age=;
run;
proc print data=newfile2;
title 'SAS dataset work.newfile';
run;
In order to run the second program, you'll need to change the path location in the FILE statement to a location where you have write access. But you should see a noticeable difference in the LOG output and then, if you open the .TXT file that is created in the second program you'll see the results of using the PUT statement.
Cynthia
I can assure you that the PUT statement works fine. It has for decades.
I do not challenge it will work. But I need to figure out what is wrong with my code.
Hi @anming I really like that @WarrenKuhfeld states "decades". 😉 I am sure PUT is older than us.
Anyways, here
put name $15. @5 age 2.;
You are stating write the values of name starting at column 1(coz this default) upto 15 bytes of character specified by the width of the format i.e. 15. Then at column 5, you are explicitly telling SAS to start writing the values of age. This means you are overwriting any value of name that is over 4 characters of length, starting with value of age in the buffer aka area of memory where SAS writes.
Correct your put statement with by removing @5 or by writing at a much greater position of the buffer, something like @16 that doesn't overwrite the name values.
put name $15. age 2.;
In the above , SAS pads the chars with blanks upto 15 column positions in the buffer. I hope this helps?
indeed the overwriting will display that 'put' works. currently, it does not overwrite the name.
I also try to input ina formatted column input.
data perm_student;
input @1 name $7.
@9 age 2. ;
cards;
afred 14
alice 13
barbara 13
carol 14
;
run;
@anming wrote:
data perm_student;
input name$ age 2.;
cards;
afred 14
alice 13
barbara 13
carol 14
;
run;
data student;
set perm_student;
put name $15. @5 age 2.;
run;
it looks that 'put' does not work. The student dataset is only a simple copy of perm_student. could you help what is wrong?
Did you look in your log for the results? If you do not specify another destination then PUT writes to the LOG.
If you expect to see the text in the results window add a File Print; statement to tell SAS to direct the output to the default output destination. If you expect the results in a different file then you need a FILE statement with the file information such as path and name.
If that doesn't help, tell us exactly why you think the Put statement did not work.
there is nothing strange in the log file.
Did you miss this in your log? This is the output from your PUT statement:
afre14 alic13 barb13a caro14
I see. These are the output in the log, which are not written to the output dataset.
@anming wrote:
I see. These are the output in the log, which are not written to the output dataset.
PUT does not write to the data set. Period.
Put writes to log, result window or output destination files.
There are no print positions in a data set to use the column modifiers like @5
If you want values in the data set that is a VARIABLE.
So, it may be time to describe what are attempting to do.
INPUT reads text into SAS which you then can assign to a column of a table.
PUT writes text to an external file and not to a data set (a table).
SET reads from a table.
DATA creates a new table.
If you want to create a new table using an existing table then the syntax is:
data <new table>; set <existing table>; run;
The SAS log is your friend so don't suppress the messages (like with NOnotes). When reading from external data (here what's under your cards statement) then it's worth to use an informat which creates your variable in the length you want to (=character with a length of 15 for name). Please note: For numerical variables the informat (2. for age) instructs SAS how to read the external text. The length for the variable will become 8 which is the internal storage used for storing the number. Do not attempt to change this length for numerical variables until you understand much better how things work.
OPTIONS NOTES SOURCE SYNTAXCHECK;
data perm_student;
input name $15. age 2.;
cards;
afred 14
alice 13
barbara 13
carol 14
;
proc contents data=perm_student;
run;
Now after all of the above: Can you clearly express what you have and what you want to achieve? How your desired result should look like?
Hi:
I am not sure why you are running this program. The PUT statement should be writing to the SAS Log, NOT to the SAS dataset. Did you look in the SAS Log after this program ran? The OUTPUT statement writes observations to the SAS dataset in the DATA statement. The PUT statement, without a FILE reference will write to the log in the same way as the PUTLOG statement. Consider these 2 DATA steps and 2 PROC PRINTS -- the differences should be obvious when you look at the PROC PRINTS and then look at the SAS log.:
data newfile;
set sashelp.class;
where age gt 14;
put @1 "** From the PUT statement: " @35 name= _n_= age=;
putlog "*** From PUTLOG statement: " name= _n_= age=;
run;
proc print data=newfile;
title 'SAS dataset work.newfile';
run;
data newfile2;
set sashelp.class;
where age gt 14;
file 'c:\temp\fromput.txt';
put @1 "** From the PUT statement: " @35 name= _n_= age=;
putlog "*** From PUTLOG statement: " name= _n_= age=;
run;
proc print data=newfile2;
title 'SAS dataset work.newfile';
run;
In order to run the second program, you'll need to change the path location in the FILE statement to a location where you have write access. But you should see a noticeable difference in the LOG output and then, if you open the .TXT file that is created in the second program you'll see the results of using the PUT statement.
Cynthia
This is a pretty old practical question. The question is "what is written to the output raw data file?". The suggested answer is
the results 'put' statement generated in the log file. This confuses me. After checking all discussions I do not think this question is well designed. Now I have a better understanding!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.