DATA Step, Macro, Functions and more

For Each Update Loop?

Accepted Solution Solved
Reply
Super Contributor
Posts: 497
Accepted Solution

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: 19,878

Re: For Each Update Loop?

Posted in reply to DavidPhillips2
data class_want;
set class2;
by sex;
retain age_filled;
if first.sex then age_filled=.;


if age ne . then age_filled=age;

run;
 

View solution in original post


All Replies
Super User
Posts: 11,343

Re: For Each Update Loop?

Posted in reply to DavidPhillips2

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.

Super Contributor
Posts: 497

Re: For Each Update Loop?

Ballardw, the 10 variables are an arrary.

Super User
Posts: 19,878

Re: For Each Update Loop?

Posted in reply to DavidPhillips2

Your question:

 

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. 

 

Otherwise see this page on arrays and how to use them:

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.

 


Super Contributor
Posts: 497

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: 19,878

Re: For Each Update Loop?

Posted in reply to DavidPhillips2
data class_want;
set class2;
by sex;
retain age_filled;
if first.sex then age_filled=.;


if age ne . then age_filled=age;

run;
 
Super Contributor
Posts: 497

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 148 views
  • 0 likes
  • 3 in conversation