BookmarkSubscribeRSS Feed
1239
Calcite | Level 5

Hi,

Please find the below sample dataset. I want for each Test whenever Visit = 1 then Visit_order to be as

   if Visit 1 then Visit_order =  1

   if Visit 14 then Visit_order = 2

   if Visit 28 then Visit_order = 3

and

   if Visit 2 then Visit_order =  4

   if Visit 14 then Visit_order = 5

   if Visit 28 then Visit_order = 6.

Could any please help me in achieving this? I have attached expected output for reference.

data test;

input Test $1 Visit 8.;

datalines;

A 1

A 14

A 28

B 2

B 14

B 28

C 2

C 14

C 28

D 1

D 14

D 28

run;

1 REPLY 1
Ksharp
Super User

Your logic is ambiguous. Why the same visit 14 28 get different visit_order?

data test;
input Test $1 Visit 8.;
datalines;
A 1
A 14
A 28
B 2
B 14
B 28
C 2
C 14
C 28
D 1
D 14
D 28
;
run;
data test;
 set test;
 retain _visit;
 if test ne lag(test) then _visit=visit;
run;
data want(drop=_visit);
 set test;
 select;
  when(_visit=1 and visit=1 ) visit_order=1;
  when(_visit=1 and visit=14) visit_order=2; 
  when(_visit=1 and visit=28) visit_order=3;
  when(_visit=2 and visit=2) visit_order=4;
  when(_visit=2 and visit=14) visit_order=5;
  when(_visit=2 and visit=28) visit_order=6;
  otherwise;
 end;
run;




Ksharp

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 1429 views
  • 0 likes
  • 2 in conversation