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

Hi !

 

 

I would like to understand the logic behind this simple data step.

 

data test ;
   put _all_ ;
   input x ;
   y = 2 * x ;
cards ;
1
;

In the log it is displayed :

 

x=. y=. _n_=1

x=. y=. _n_=2

 

I probably "missing" few details :

 

1. Why are there 2 iteration ?

2. Is the first iteration done  for reading the data descriptor ?

3. Why then at the second iteration (when reading INPUT i guess ) X and Y are still missing ?

 

 

Thanks in advance

 

saskap

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

The datastep iteration halts when a SET of INPUT statement reads past the end of file. Any statements before that (in this case a PUT statement) are still executed.

 

2016-03-30 15_29_46-Introduction to DATA Step Processing_ How the DATA Step Works_ A Basic Introduct.png

 

Note that your output dataset still has only one observation with X=1 and Y=2, as expected.

 

- Jan.

View solution in original post

6 REPLIES 6
jklaverstijn
Rhodochrosite | Level 12

What you see is because the put _all_ statement is before the input. The datastep halts when the input statement reads end of data in the cards. That's only after you print _all_ to the log.

 

Also, because the put statement comes before any other statements all variables are missing. At the start of an iteration the PDV is cleared for all non-retained variables.

 

For illustration you could move the put statement to the botrtom of the datastep after the assigment tof Y.

 

Hope this clariefies things a bit.

- Jan

jklaverstijn
Rhodochrosite | Level 12

For the basics have a good look at How the DATA Step Works: A Basic Introduction. It is essential learning.

 

- Jan.

saskapa
Quartz | Level 8

Thanks Jan...Yes off course the reinitialisation of the PDV...!   However, why two iteration ? My only explanation would be that the first iteration creates  data descripor (compilation section and _n_=1)  and that the second iteration only it starts the execution...What do you think ? 

jklaverstijn
Rhodochrosite | Level 12

The datastep iteration halts when a SET of INPUT statement reads past the end of file. Any statements before that (in this case a PUT statement) are still executed.

 

2016-03-30 15_29_46-Introduction to DATA Step Processing_ How the DATA Step Works_ A Basic Introduct.png

 

Note that your output dataset still has only one observation with X=1 and Y=2, as expected.

 

- Jan.

Tom
Super User Tom
Super User

@saskapa wrote:

Thanks Jan...Yes off course the reinitialisation of the PDV...!   However, why two iteration ? My only explanation would be that the first iteration creates  data descripor (compilation section and _n_=1)  and that the second iteration only it starts the execution...What do you think ? 


No.  All data steps are compiled before they start.  

The reason there are two put statements is because the implied data step loop runs twice.  The second time it stops when the INPUT statement reads past the end of the input.  So it never gets to the end where the record is written so you only have one output record. This is how all simple data steps work.  The same would happen if you were using a SET statement instead of reading raw data with an INPUT statement. When the SET statement reads past the end of the data then the step stops.

saskapa
Quartz | Level 8

Thanks Tom for the clarification

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 2327 views
  • 4 likes
  • 3 in conversation