Help using Base SAS procedures

Looping with variables

Reply
N/A
Posts: 0

Looping with variables

hi

i recently started programming in SAS and i have a couple of questions.

assuming that i have a couple of variables with a number of observations, how do i write a loop so that a procedure is executed on every variable?

for example. i have variables named Y1, Y, .. ,Yn. how do i write a loop so that an if statement is performed for all the variables. so i don't have to type:

if Y1 = x then DELETE;
if Y2 = x then DELETE;
...
if Yn = x then DELETE;


and my second question is if it's possible to print variables from more than one data set using proc print? cus if i have two DATA = the log tells me

WARNING: Ignoring second data set reference
Super Contributor
Super Contributor
Posts: 3,174

Re: Looping with variables

Posted in reply to deleted_user
You can declare an ARRAY statement with a specific set of SAS variable(s) and iterate over the array with a DO / END loop, as shown below:

data _null_;
retain charvar1-charvar5 'x';
ARRAY acharvar (*) $ charvar1 charvar3 charvar5;
do i=1 to dim(acharvar);
if acharvar(i) = 'x' then putlog 'match - ' acharvar(i)= ;
end;
run;

And PROC PRINT cannot be used with more than one data file at a time.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: Looping with variables

You can define a view thus:

DATA TEMP / VIEW=TEMP;
SET FILEA FILEB;
RUN;

PROC PRINT DATA=TEMP;
RUN;

Of course, the two files should have compatible variables etc for this to work.
Ask a Question
Discussion stats
  • 2 replies
  • 126 views
  • 0 likes
  • 2 in conversation