BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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

6 REPLIES 6
ballardw
Super User

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.

DavidPhillips2
Rhodochrosite | Level 12

Ballardw, the 10 variables are an arrary.

Reeza
Super User

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.

 


DavidPhillips2
Rhodochrosite | Level 12

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.

Reeza
Super User
data class_want;
set class2;
by sex;
retain age_filled;
if first.sex then age_filled=.;


if age ne . then age_filled=age;

run;
 
DavidPhillips2
Rhodochrosite | Level 12

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 1382 views
  • 0 likes
  • 3 in conversation