BookmarkSubscribeRSS Feed
Inp
Obsidian | Level 7 Inp
Obsidian | Level 7

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.

5 REPLIES 5
ballardw
Super User

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;

 

Inp
Obsidian | Level 7 Inp
Obsidian | Level 7

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

 

Thanks again.

MikeZdeb
Rhodochrosite | Level 12

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;

Astounding
PROC Star

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.

Inp
Obsidian | Level 7 Inp
Obsidian | Level 7

Thanks very much , it cleared my confussion.

 

 

Thanks

 

Inp

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, 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
  • 5 replies
  • 1057 views
  • 0 likes
  • 4 in conversation