BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
akmm10
Calcite | Level 5

I have a wide dataset (could transpose to long, then be re-transposed if necessary) that has various outcomes at 15 time points. If missing, it will have "NA." I want to impute the first missing "NA" right after an observation. See example:

 

HAVE:

idtime1time2time3time4time5
A1NA2NANA
B23NA3NA
C2NANANA4
D122NANA

 

WANT:

idtime1time2time3time4time5
A1122NA
B23333
C22NANA4
D1222NA

 

Any help would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just work from right to left.

data have;
  input id $ (time1-time5) (:$2.);
cards;
A 1 NA 2 NA NA
B 2 3 NA 3 NA
C 2 NA NA NA 4
D 1 2 2 NA NA
;

data want;
  set have;
  array t time1-time5;
  do i=dim(t) to 2 by -1;
    if t[i]='NA' then t[i]=t[i-1];
  end;
  drop i;
run;

Result

Tom_0-1744657382583.png

 

Although I do not understand why you would want those NA strings in your TIME variables when the other values look like numbers.  If instead of that constant text you set the values MISSING then you could use MISSING() function instead of the equality operator in the IF condition.

if missing(T[i]) then ...

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Just work from right to left.

data have;
  input id $ (time1-time5) (:$2.);
cards;
A 1 NA 2 NA NA
B 2 3 NA 3 NA
C 2 NA NA NA 4
D 1 2 2 NA NA
;

data want;
  set have;
  array t time1-time5;
  do i=dim(t) to 2 by -1;
    if t[i]='NA' then t[i]=t[i-1];
  end;
  drop i;
run;

Result

Tom_0-1744657382583.png

 

Although I do not understand why you would want those NA strings in your TIME variables when the other values look like numbers.  If instead of that constant text you set the values MISSING then you could use MISSING() function instead of the equality operator in the IF condition.

if missing(T[i]) then ...

 

akmm10
Calcite | Level 5

Oh, simpler than I thought! Thanks!

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 219 views
  • 2 likes
  • 2 in conversation