Solved
Valued Guide
Posts: 610

# For Each Update Loop?

In SAS is there an equivalent of

For each employee id do;

LastVariable;

Variable[1-10];

For I =1 to I = 10 do;

If variable(i) is null then  then Variable(i) = LastVariable;

End;

End;

I’m trying to update a set of records using the last real value where the value to fill null values further in the series.

Accepted Solutions
Solution
‎08-23-2017 09:04 AM
Super User
Posts: 23,353

## Re: For Each Update Loop?

``````data class_want;
set class2;
by sex;
retain age_filled;
if first.sex then age_filled=.;

if age ne . then age_filled=age;

run;
``````

All Replies
Super User
Posts: 13,358

## Re: For Each Update Loop?

Providing a little example data and the desired result will help. "Last variable" without some context may take a lot of verbiage where example data is more concrete. You would not need to provide all 10 variables, 3 or 4 should suffice to show the logic needed.

Valued Guide
Posts: 610

## Re: For Each Update Loop?

Ballardw, the 10 variables are an arrary.

Super User
Posts: 23,353

## Re: For Each Update Loop?

In SAS is there an equivalent of

For each employee id do;

LastVariable;

Variable[1-10];

For I =1 to I = 10 do;

If variable(i) is null then  then Variable(i) = LastVariable;

End;

End;

My answer: Yes, SAS has an equivalent. Very close to your 'code' as well.

If you need help on how to code this post more detailed information, including sample data and expected output.

https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

I don't think any of them exactly match your situation but they should give you an idea.

Valued Guide
Posts: 610

## Re: For Each Update Loop?

data class2; set sashelp.class;
if weight > 100 then age = .;

run;

proc sort data=class2;
by sex descending age ;
run;

data class2; set class2;
if age = 12 then age = .;
run;

For both genders, I am trying to fill the age with the age before it if it is blank.

Solution
‎08-23-2017 09:04 AM
Super User
Posts: 23,353

## Re: For Each Update Loop?

``````data class_want;
set class2;
by sex;
retain age_filled;
if first.sex then age_filled=.;

if age ne . then age_filled=age;

run;
``````
Valued Guide
Posts: 610

## Re: For Each Update Loop?

Minor enhancement:

data class2; set sashelp.class;
if weight > 100 then age = .;

run;
proc sort data=class2;
by sex descending age ;
run;
data class2; set class2;
if age = 12 then age = .;
run;

data class_want (drop=age rename=(age_filled =age) );
set class2;
by sex;
retain age_filled;
if first.sex then age_filled=.;

if age ne . then age_filled=age;

run;

☑ This topic is solved.