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

 

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

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

View solution in original post

14 REPLIES 14
WarrenKuhfeld
Rhodochrosite | Level 12

I can assure you that the PUT statement works fine. It has for decades.

anming
Pyrite | Level 9

I do not challenge it will work. But I need to figure out what is wrong with my code.

Cynthia_sas
SAS Super FREQ
Hi, As Warren explains, the PUT statement works fine -- it was one of the first statements I had to learn because my first job using SAS required me to write TEXT files that could be imported into a different mainframe application. The job of the PUT statement, however, is NOT to write to a SAS dataset. The job of the PUT statement is to write values (text strings, variable values) to a text location such as the SAS Log, an ASCII text file or sometimes, to a command stream (such as the old days with DDE). In the "old" days, the PUT statement wrote to the SAS log in the absence of any other instructions. With a File reference, the PUT statement can write to a "flat" file or "raw" file or ASCII text file. Later, the PUTLOG statement was designed to make it explicit where text and values were being written.

I suspect you did not look in the SAS Log after you ran your program. Please read the documentation for the PUT statement. Your assumptions about how you write to a SAS dataset versus how you write to a flat file versus how you write to the SAS Log need clarifying and I have posted some code that should help you understand better. However, you will need to review the SAS Log and the SAS dataset that's created and the PROC PRINT output in order to really zero in on what is happening with the PUT statement.
Cynthia
novinosrin
Tourmaline | Level 20

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?

 

 

 

anming
Pyrite | Level 9

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;

 

ballardw
Super User

@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.

anming
Pyrite | Level 9

there is nothing strange in the log file.

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 data student;
74 set perm_student;
75 put name $15. @5 age 2.;
76 run;
 
afre14
alic13
barb13a
caro14
NOTE: There were 4 observations read from the data set WORK.PERM_STUDENT.
NOTE: The data set WORK.STUDENT has 4 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
 
77
78
79 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
91
SASKiwi
PROC Star

Did you miss this in your log? This is the output from your PUT statement:

afre14
alic13
barb13a
caro14
anming
Pyrite | Level 9

I see. These are the output in the log, which are not written to the output dataset. 

ballardw
Super User

@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.

Patrick
Opal | Level 21

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;

Patrick_0-1609375872436.png

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?

 

 

 

Cynthia_sas
SAS Super FREQ

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

anming
Pyrite | Level 9

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!

Cynthia_sas
SAS Super FREQ
Hi:
If this question came from one of our SAS Education classes or from the certification Prep Guide or from the practice exam that is published by SAS, then please post EXACTLY where you found this question in our material so we can review it.
If this question came from one of the 3rd party sites that you referred to in a previous posting, then this is the reason we don't recommend using web sites not sponsored by SAS.
Please look again at the Content Guide for the Base Programmer exam, although the PUTLOG statement and the PUT function are covered on the Base Programmer exam, I believe you will find that the simple PUT statement is NOT covered on the exam. The Content Guide makes it clear what will be covered on the exam.
Cynthia

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!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 14 replies
  • 2648 views
  • 5 likes
  • 7 in conversation