Please explain the required logic a bit more in detail - especially why seq remains missing for the first and last obs in your sample data.
Sorry for not being so specific - below is a clear explanation. Data Have is how my dataset looks like and Data want is how I want my data too look like.
Because I have collapsed a record highlighted in green, I want to flag all the associated date records which fall from the 02-Dec-17 to 16-Jan-18. My question is how do I retain the collapsed and the count variable at observation 5&6 of the seq variable.
So you simply want to carry forward the last non-missing observation?
data want;
set have;
by subjid;
retain _coll _cnt;
if first.subjid
then do;
_coll = .;
_cnt = .;
end;
if collapsed ne .
then _coll = collapsed;
else collapsed = _coll;
if count ne .
then _cnt = count;
else count = _cnt;
drop _:;
run;
Untested, for lack of usable example data.
So you have now given us some more information - namely about the position of SEQ.
You have NOT stated why collapsed and count are propagated from obs 4 through 5 and 6, but obs 7 is not propagated. Is it because ex='Y' in obs 4, but not in obs 7?
When does propagation stop? In the above case, obs 7 has data that stops propagation of data from obs 4, but what if there were no such data after obs 4? When would propagation stop, if at all?
Also you show only only one subjid in your data. You didn't say whether data should be propagated across subjects (probably not, but it would be good to have it stated).
In short, help us help you - by providing a sufficient description of the problem such that you won't have to answer lots of follow-up questions. That's probably a more valuable skill to develop than sas coding, even in this forum.
Hi,
Any idea how I can get the missing observation where highlighted in seq variable 5&6, but it should not retain the values in seq 8 and 9 of my orignal post.
I tried to use retain statement but it does not provide the output I want.
I am sharing another example below - I only want collap, count, collapse where highlighted in yellow. The reason behind is because I have collapsed dates where collapex =Y . All the dates associated with the collapsing should have collap = 1, count =1 or count =2 if a second collapsing of date has happened within same subject and collapse = Y.
Hope this is clear. Apart from retain what is the best method I can use?
Please post your data in usable form, which means a data step with datalines, which we can copy/paste to our environment and submit.
Test your data step code before posting to make sure it recreates a sufficient example of your data, and use the "little running man" button to open a code box into which you paste the code.
I think you need to make a working data step to show the initial condition of your data. It appears that SEQ is missing for obs 1 and 7. Instead you have collapse=1 for the first obs and collapse=7 for the last.
But it that is not your intention, then you should show missing values for collapse and count (two .'s between subjid and seq).
If I am correct (this is untested until in absence of actual startup data in a data step), then this code should work.
data want (drop=_:);
set have;
retain _last_collapse _last_count;
if collapse^=. then _last_collapse=collapse;
if count^=. then _last_count=count;
if collapse=. then collapse=_last_collapse;
if count=. then count=_last_count;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.