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.
data class_want;
set class2;
by sex;
retain age_filled;
if first.sex then age_filled=.;
if age ne . then age_filled=age;
run;
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.
Ballardw, the 10 variables are an arrary.
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.
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.
data class_want;
set class2;
by sex;
retain age_filled;
if first.sex then age_filled=.;
if age ne . then age_filled=age;
run;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.