What does these codes do?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

What does these codes do?

 

data _NULL_ (drop=TeamName);
length Event3 Event2 Event1 8. TeamName ParticipantName $8. ;
format Event2 z8. ;
retain TeamName Count ;

input TeamName $ Count @ ;

do i = 1 to Count ;
input ParticipantName $ Event1 Event2 Event3 @@ ;
TeamTotal + (Event1 + Event2 + Event3);
output ;

FILE 'C:\Users\user\Desktop\IDS SAS 594\samplefile.txt' ;
if _N_=3 then do ;
if i = 1 then do ;
put TeamName Count @@ ;
end ;
if i < 2 then do ;
put ParticipantName Event1 Event2 Event3 @@ ;
end ;
else do ;
put ParticipantName Event1 Event2 Event3 ;
end ;
end ;
else do ;
put TeamName Count ;
put ParticipantName Event1 Event2 Event3 ;
end ;
end ;

datalines;
Knights 1
Sue 6 8 8
Kings 1
Jane 9 7 8
Knights 4 John 7 7 7 Lisa 8 9 9
Fran 7 6 6
Walter 9 8 10
;

 

I do not understand the bold parts.

 

Help 


Accepted Solutions
Solution
‎04-08-2016 08:29 AM
Esteemed Advisor
Posts: 6,646

Re: What does these codes do?

Next, look at these pieces of code:

do i = 1 to Count;
  .......

  if _N_ = 3 then do;
    if i = 1 then do;
      put TeamName Count @@;
    end;
    if i < 2 then do;
 

One can easily see that the variable i will only have ordinal values of 1,2,3,.....

Therefore the condition i < 2 is equivalent to i = 1, and the two blocks can be merged into one with the same functionality, which would also improve readability.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Grand Advisor
Posts: 10,210

Re: What does these codes do?

Retain creates and identifies a variable whose values are kept from one observation (record) to the next.

_N_ is an automatic variable that SAS supplies to identify the record .

So if _N_=3 then do  is "When this is the third record do something"

 

You have a group of statements done for values of 1 to the value of count on the current record that starts with : do i = 1 to Count ; to the END; just before datalines.

 

if i < 2 then do ; If Count is 2 or larger then the loop will increment and the counter variable i, when the counter is less than 2 point it prints  the values of some variables to the log. 

 

Since i will have values like 1 , 2 ,3 the same effect could be done with i=1 to print on the first pass through the loop.

Occasional Contributor
Posts: 10

Re: What does these codes do?

Concerning "if _N_ = 3 do"

 

But I thought that SAS Data step updates _N_ after the iteration. 

 

Meaning when the data step iterates to the 3rd recound, _N_ is still equal to 2. Only after finishing the 3rd record do _N_ gets updated to 3?

Trusted Advisor
Posts: 1,114

Re: What does these codes do?

[ Edited ]

 

Actually, the interpretation of _N_ is a bit tricky here: It counts the iterations of the data step. In many cases this coincides with the "record number," as @ballardw pointed out. In the present data step, however,

  • _N_=1 for the first two input records, which in turn go into the first output record (replace _NULL_ by a dataset name to check), but correspond to the first two records in samplefile.txt.
  • _N_=2 for the 3rd and 4th input record, which in turn go into the second output record and which correspond to the 3rd and 4th line in samplefile.txt.
  • _N_=3 for the 5th, 6th and 7th input record, which in turn populate output records no. 3, 4, 5 and 6 and are written to lines 5, 6 and 7 of samplefile.txt.
  • Edit: The very last value of _N_ is even 4: immediately before the first INPUT statement of the data step hits the semicolon after the last data line, which terminates the data step.

(See documentation on "Automatic Variables.")

 

Just for completeness: The single trailing @ at the end of the first INPUT statament "holds an input record for the execution of the next INPUT statement within the same iteration of the DATA step" (INPUT statement documentation). This is relevant here when the fifth data line is being read. After reading "Knights 4" the pointer must stay on the same input record to let the next INPUT statement read "John" etc. and not "Fran" (ignoring the rest of the previous line).

 

Esteemed Advisor
Posts: 6,646

Re: What does these codes do?

All posters:

Look thoroughly at that data step, it reads from DATALINES and writes to the file in the FILE statement. What is especially puzzling is that a keep/drop option is used on _NULL_ and a completely useless OUTPUT statement is present.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Posts: 6,646

Re: What does these codes do?

First, put some proper formatting into this horrible piece of spaghetti code:

data test /*(drop=TeamName)*/;
file '$HOME/sascommunity/samplefile.txt' ;
length Event3 Event2 Event1 8. TeamName ParticipantName $8.;
format Event2 z8.;
retain TeamName Count;

input TeamName $ Count @;

do i = 1 to Count;
  input ParticipantName $ Event1 Event2 Event3 @@;
  TeamTotal + (Event1 + Event2 + Event3);
  output;

  if _N_ = 3 then do;
    if i = 1 then do;
      put TeamName Count @@;
    end;
    if i < 2 then do;
      put ParticipantName Event1 Event2 Event3 @@;
    end ;
    else do ;
      put ParticipantName Event1 Event2 Event3;
    end;
  end;
  else do;
    put TeamName Count;
    put ParticipantName Event1 Event2 Event3;
  end;
end;

datalines;
Knights 1
Sue 6 8 8
Kings 1
Jane 9 7 8
Knights 4 John 7 7 7 Lisa 8 9 9
Fran 7 6 6
Walter 9 8 10
;
run;

Now it becomes easier to see that the if _N_ = 3 block is only there to recreate the layout of the datalines in the output file (more or less, as event2 now has a z8. format).

Does this have ANY practical use?

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 10

Re: What does these codes do?

No, this piece of code is written by my professor for demonstration purposes. It has no practical use. It is there to show us how to use a double trailing @@ to input the datalines as shown.

Solution
‎04-08-2016 08:29 AM
Esteemed Advisor
Posts: 6,646

Re: What does these codes do?

Next, look at these pieces of code:

do i = 1 to Count;
  .......

  if _N_ = 3 then do;
    if i = 1 then do;
      put TeamName Count @@;
    end;
    if i < 2 then do;
 

One can easily see that the variable i will only have ordinal values of 1,2,3,.....

Therefore the condition i < 2 is equivalent to i = 1, and the two blocks can be merged into one with the same functionality, which would also improve readability.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 434 views
  • 0 likes
  • 4 in conversation