BookmarkSubscribeRSS Feed
Rahul_SAS
Quartz | Level 8

Hi Experts,

 

I am unable to understand how this program is giving output for 8 observation and repeating 1, 2 and 5???

 

data input;
infile datalines;
input Var1 $ Var2 $;
datalines;
A one
A two
B three
C four
A five
;
RUN;
data one two;
set WORK.INPUT;
count= _n_;
if Var1='A' then output WORK.ONEs;
output;
run;

2 REPLIES 2
gamotte
Rhodochrosite | Level 12

Hello,

 

Data step processes the input dataset row by row.

If there is no explicit output instruction in the code,

the currently processed row (*) is output when the interpreter

hits the run instruction. If there remains rows to process, there

is an implicit loop and the instructions in the data step are

executed again for the next row of the input dataset.

 

(*) More precisely the program data vector (PDV) wich is SAS terminology to

indicate the data being processed (row extracted from the input dataset + newly

created variables).

 

 

On the other hand, when the code in the data step contains some

output instructions, those determine when to output new lines in the

created dataset and the run instruction does not implicitely output 

data anymore.

 

So in your code, for each input row where Var1='A', the instruction

output WORK.ONE

creates a new row in table ONE.

The following output instruction creates a new row in both output datasets.

 

With 3 rows having Var1='A' and two rows where Var1 <> 'A',

the ONE dataset will have : 3x2+2=8 rows.

Reeza
Super User

There are two output statements. 

 

The one executes for ALL records. The second is conditional and only executes if the variable is A, records 1,2 and 5 in your case. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 665 views
  • 0 likes
  • 3 in conversation