DATA Step, Macro, Functions and more

Question about using line pointer and using trailing sign to read file

Reply
Frequent Contributor
Frequent Contributor
Posts: 81

Question about using line pointer and using trailing sign to read file

I wrote the below  program using line pointer and it worked as I expected. but still bothering my mind if this program is correct since I use the 'IF' statment between line 1 pointer  and line2 pointer. every time when SAS see the Input statment , it loads the data to PDV.  Since It has two input statment, how come it out put t1 value . I thought t1 will be disappear when it see the seond input.  I am trying to understand how it works.

data temp;

input #1 t1 $1. @2 t2 1.;

if t1 in ('a','b') ;

input

#2 t3 $1.

#3 t4 $1.

;

cards;

a1

b

c

d1

e

f

g1

h

i

a5

w

2

;

run;

 

I create the below program using the trailing sign and it worked as well as I expected.

 

data temp;

input @1 t $1. @;

if t in ('a','b') ;

input

#1 t1 $1. @2 t2 1.

#2 t3 $1.

#3 t4 $1.

;

cards;

a1

b

c

d1

e

f

g1

h

i

a5

w

2

;

run;

 

I want to know which is more correct one based on the SAS programming concept. I like the idea of using line pointer, can any one send me the link of how line pointer works. Also please explain how my first program works.

 

thanks very much in advance for your help.

 

Tony.

Super User
Posts: 11,343

Re: Question about using line pointer and using trailing sign to read file

To determine "which one is more correct" we would need to know what the desired output is.

Your code below will always have all of the variables mentioned in the code. Any specific variables that you do no want would be referenced in a DROP statement to drop them from the output: DROP t1;, or alternately use a KEEP to list the variables you want in the output: KEEP t2 t3 t4; However Keep and Drop are not executed conditionally. The data set will either have a column for the variable or not. The values can be conditional though.

 

Without an explicit OUTPUT statement the state of the data vector when the code reaches the end of the statements is what is written to the output data set. Output can be conditional: If t1 = 3 then output; would send the state of the data vector to the output at that point in the code. Note that if you have an explicit output such as this, then only where you have an output statement will the data be sent to the output data set.

Try modifying your code as below to see the values of the variables and that may help you see what is going on.

data temp;
input #1 t1 $1. @2 t2 1.;
put 'Place 1' _all_;
if t1 in ('a','b') ;
input
#2 t3 $1.
#3 t4 $1.
;
put 'Place 2' _all_;
cards;

 

Frequent Contributor
Frequent Contributor
Posts: 81

Re: Question about using line pointer and using trailing sign to read file

Thanks Balladw, The put _all_ statement helps to understand  what is actually occuring on every step.

 

Thanks again.

Valued Guide
Posts: 765

Re: Question about using line pointer and using trailing sign to read file

[ Edited ]

Hi.  Since there's no instance in your data where you'd find t1='B', this also works given the data structure ...

 

data temp;
retain t1 'a';
input @ ('a') t2 (t3 t4) (:$1.) ;
datalines;
a1
b
c
d1
e
f
g1
h
i
a5
w
2
;

 

Also, the natural behavior of LIST input is to keep reading values (even on another line of the input file) until the INPUT statement is satisfied.  So, you can get rid of the line pointers and try ..

 

data temp;
input t1 $1. t2 1.;
if t1 in ('a','b');
input (t3 t4) (:$1.);
datalines;

Super User
Posts: 5,503

Re: Question about using line pointer and using trailing sign to read file

It's a small thing, but this is a hair more direct.  Read in the minimum amount of information needed to subset, and read it into t1 so the same data doesn't have to be read again:

 

data temp;

input #1 t1 $1.  @ ;

if t1 in ('a','b') ;

input

 t2 1.

#2 t3 $1.

#3 t4 $1.

;

cards;

a1

b

c

d1

e

f

g1

h

i

a5

w

2

;

 

It's another tiny thing, but note that the semicolon at the end of the data triggers the DATA step to run.  You don't need to add a RUN; statement.

Frequent Contributor
Frequent Contributor
Posts: 81

Re: Question about using line pointer and using trailing sign to read file

Posted in reply to Astounding

Thanks very much , it cleared my confussion.

 

 

Thanks

 

Inp

Ask a Question
Discussion stats
  • 5 replies
  • 290 views
  • 0 likes
  • 4 in conversation