BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

Hello:

 

In PDV of first program, every observation occurs three times, but only first observation in second program. Although the output is same.  I think it is  because of OUTPUT statement in first program. Just need verification from the experts.

 


data test;
    put _all_;
	set sashelp.class;
	put _all_;
	if name = 'Alfred' then output;
	put _all_;
run;

data test;
	put _all_;
	set sashelp.class;
	put _all_;
	if name = 'Alfred';
	put _all_;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

I agree with @SuryaKiran, but the observed behavior isn't limited to just subsetting if statements. The following is equivalent:

data test;
    put _all_;
    set sashelp.class;
    put _all_;
    if name NE 'Alfred' then delete;
    put _all_;
run;

Art, CEO, AnalystFinder.com

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

The put statements in your datastep only print the values, from the pdf, to your log. They have no effect on the file you are creating with the datastep.

 

In your first datastep you are explicitly only outputting the record that has the name 'Alfred', thus you only get one record.

 

In your second datastep, each record is output, since that is how SAS functions if a datastep doesn't include a specific output statement.

 

If you look at your log, you'll notice that the first put statement produces a record in your output that shows all missing values. That is because no record had yet been read.

 

The reason that the last record also has three non-missing representations in the log is because (1) the first put statement lists (in the log) the values for the previous person, (2) the next two put statements list the values (in the log) for the last record and, then (3) SAS goes to read another record, list the values (in the log) of the last record, encounters the end of file upon executing the set statement, and stops processing at that point.

 

Art, CEO, AnalystFinder.com

 

 

SAS_inquisitive
Lapis Lazuli | Level 10

@art297 My question was why first data step program has three representations  for all observations in the log while second data step program has three representation of first observation and two representation for the  rest. 

 

My intuition is -  IF statement is true for the first observation, so the last PUT statement executes in the second program. While IF statement is FALSE for the rest and the last PUT statement never executes. 

 

In first program, the last PUT statement executes for all observations, even IF condition is true for the first observation only. The observations are still available beyond OUTPUT statement.

art297
Opal | Level 21

I agree with @SuryaKiran, but the observed behavior isn't limited to just subsetting if statements. The following is equivalent:

data test;
    put _all_;
    set sashelp.class;
    put _all_;
    if name NE 'Alfred' then delete;
    put _all_;
run;

Art, CEO, AnalystFinder.com

SuryaKiran
Meteorite | Level 14

Your 2nd program is called sub-setting IF(without THEN clause) , the process will continue only if the statement is true. Only first record is true so all three PUT statements executed but for other records the third PUT statement will not execute. 

Thanks,
Suryakiran

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 4 replies
  • 1651 views
  • 3 likes
  • 3 in conversation