BookmarkSubscribeRSS Feed
gahlot1999
Fluorite | Level 6

Hi Community,

My source data is - 

gahlot1999_0-1652732566507.png

And I want this output - 

gahlot1999_1-1652732645715.png

Any easy way to do this in proc sql?

2 REPLIES 2
gahlot1999
Fluorite | Level 6
I want seq variable to get change when unscheduled visit is there...otherwise it should do +1 always.
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1106 views
  • 0 likes
  • 2 in conversation