BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ajayvit
Obsidian | Level 7

Question;

data sv;
input visit$;
datalines;
cycle1
cycle2
unsch
unsch
cycle3
unsch
unsch
cycle4
cycle5
;
run;

 

Expecting;


visit      Number
------      ------
cycle1   1
cycle2   2
unsch    2.1
unsch    2.2
cycle3   3
unsch    3.1
unsch    3.2
cycle4   4
cycle5   5
___________________________________

Code I used(but not giving expected result)

 

data visit;
set sv;
if visit in ("cycle") then seq=1;
else seq+1;
retain seq;
if visit in("unsch") then seq=seq+3;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data visit;
set sv;
retain cy 0 subcy;
if find(visit,'cycle')
then do;
  cy + 1;
  subcy = 0;
end;
else subcy + 1;
number = catx(".",cy,ifc(subcy,put(subcy,best.),""));
keep visit number;
run;

Untested, posted from my tablet.

View solution in original post

7 REPLIES 7
Reeza
Super User

IN allows you to avoid multiple OR conditions, it doesn't check for portion of a string. You want FIND, INDEX, CONTAINS or the colon operator. =: will check if the strings start with the text cycle. 

 

data visit;
set sv;

retain seq 0;

if visit =: ("cycle") then seq+1;
else seq + 0.1;

format seq 8.1;
run;

@Ajayvit wrote:

Question;

data sv;
input visit$;
datalines;
cycle1
cycle2
unsch
unsch
cycle3
unsch
unsch
cycle4
cycle5
;
run;

 

Expecting;


visit      Number
------      ------
cycle1   1
cycle2   2
unsch    2.1
unsch    2.2
cycle3   3
unsch    3.1
unsch    3.2
cycle4   4
cycle5   5
___________________________________

Code I used(but not giving expected result)

 

data visit;
set sv;
if visit in ("cycle") then seq=1;
else seq+1;
retain seq;
if visit in("unsch") then seq=seq+3;
run;


 

Ajayvit
Obsidian | Level 7
Dear Rezza,

Thank you for your help.
My expectation is:
visit Number
------ ------
cycle1 1 or 1.0
cycle2 2 or 2.0
unsch 2.1
unsch 2.2
cycle3 3 or 3.0
unsch 3.1
unsch 3.2
cycle4 4 or 4.0
cycle5 5 or 5.0

What I am getting is :
visit Number
------ ------
cycle1 1.0
cycle2 2.0
unsch 2.1
unsch 2.2
cycle3 3.2
unsch 3.3
unsch 3.4
cycle4 4.4
cycle5 5.5


for cycle3 cycle4 cycle5 not getting the expected value.
Kurt_Bremser
Super User
data visit;
set sv;
retain cy 0 subcy;
if find(visit,'cycle')
then do;
  cy + 1;
  subcy = 0;
end;
else subcy + 1;
number = catx(".",cy,ifc(subcy,put(subcy,best.),""));
keep visit number;
run;

Untested, posted from my tablet.

Ajayvit
Obsidian | Level 7

Dear Kurt,

 

Thank you so much for your kind help.

 

Regards,

Ajay

Ajayvit
Obsidian | Level 7

Hi Kurt,

I took the reference to your code and did some modifications.

This is also giving the same result.

 

data sv_1;
set sv;
if find(visit,"cycle") then do;
sq+1;
nd=0;
end;
else nd+1;
seq=catx(".",sq,nd);
keep visit seq;
run;

Astounding
PROC Star

I know you have a working solution now, but the problem struck me as much simpler:

 

data sv_1;
set sv;
if visit = 'unsch' then seq + 0.1;
else seq = int(1 + seq);
run;

Of course, all solutions run into trouble if there are 10 unscheduled visits in a row.

Kurt_Bremser
Super User

@Astounding wrote:

 

Of course, all solutions run into trouble if there are 10 unscheduled visits in a row.


Mine won't, as I build the result string from two integers, so 1.10 is followed by 1.11.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 7 replies
  • 1684 views
  • 1 like
  • 4 in conversation