Hi Community,
My source data is -
And I want this output -
Any easy way to do this in proc sql?
I imagine one could develop some way to torture SQL into submission for this task - although I am unable to see how.
But why? The DATA step cares about the sequence of incoming records, while SQL doesn't - that's SQL's advantage/disadvantage. True, in SQL you could detect "Screen","Week 1", "Week 2", "Week 3" and "End of study" to generate codes, 1,2,3,4 and 5. That's assuming you want those codes for each visit type.
But what about "Unscheduled?" Could such a record fall between "Week 2 and "Week 3", implying codes of 3.1, 3.2, etc instead of 4.1, 4.2? How would SQL determine where the Unscheduled appears? And then you have demonstrated the problem of multiple Unscheduled records. Which one gets 4.1 and which gets 4.2 (assuming you even know they should be a 4.x to begin with).
This data step is untested, in the absence of sample data in the form of a working data step:
data want;
set have;
by id;
if seq='Unscheduled' then seq+0.1;
else seq=ceil(seq+0.01);
if first.id then seq=1;
run;
Of course, this assumes you never have more then 9 consecutive "Unscheduled" records.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.