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

Hi SAS expert,

 

My apology for not being able to better define the Subject. I’ll appreciate you help regarding the following:

 

I have a variable named InstanceName, that has the following values
Follow-up 01,
Follow-up 02,
Follow-up 03,
….
so I need to get another variable that will have (numerical) value 1 if InstanceName=Follow-01, value 2 if InstanceName=Follow-up 02, 3 if InastanceName=Follow-up 03 etc, and this is easy to do using proc sql, and conditional statement “case when … then … else…end” or SAS data statement...
The problem is that Folllow-up xx number is changing, and within different data sets might be up 4, while within other data sets might be 15 and up.
So is it possible to make this “case when… then… else end” statement more data dependent, so I don’t always have to check how may Follow-up xx are there for each set of data, and that SAS runs this up the last Follow-up xx available?


Here how the code, in general, look like:

proc sql;
create table Table_1 as
select Var1, Var2, case
when InstanceName="Follow-up 01" then 1
when InstanceName="Follow-up 02" then 2
when InstanceName="Follow-up 03" then 3
….
else ...
end as FUP_NUM
from Data_1;
quit;

 

Thank you very much for your time,
Batta

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

No Case/when needed:

 

input(scan(Instance_name,2,' '),f2.) as Fup_num

the Scan gets the part after the space and then Input reads it as a number.

 


@Batta wrote:

Hi SAS expert,

 

My apology for not being able to better define the Subject. I’ll appreciate you help regarding the following:

 

I have a variable named InstanceName, that has the following values
Follow-up 01,
Follow-up 02,
Follow-up 03,
….
so I need to get another variable that will have (numerical) value 1 if InstanceName=Follow-01, value 2 if InstanceName=Follow-up 02, 3 if InastanceName=Follow-up 03 etc, and this is easy to do using proc sql, and conditional statement “case when … then … else…end” or SAS data statement...
The problem is that Folllow-up xx number is changing, and within different data sets might be up 4, while within other data sets might be 15 and up.
So is it possible to make this “case when… then… else end” statement more data dependent, so I don’t always have to check how may Follow-up xx are there for each set of data, and that SAS runs this up the last Follow-up xx available?


Here how the code, in general, look like:

proc sql;
create table Table_1 as
select Var1, Var2, case
when InstanceName="Follow-up 01" then 1
when InstanceName="Follow-up 02" then 2
when InstanceName="Follow-up 03" then 3
….
else ...
end as FUP_NUM
from Data_1;
quit;

 

Thank you very much for your time,
Batta


 

View solution in original post

2 REPLIES 2
ballardw
Super User

No Case/when needed:

 

input(scan(Instance_name,2,' '),f2.) as Fup_num

the Scan gets the part after the space and then Input reads it as a number.

 


@Batta wrote:

Hi SAS expert,

 

My apology for not being able to better define the Subject. I’ll appreciate you help regarding the following:

 

I have a variable named InstanceName, that has the following values
Follow-up 01,
Follow-up 02,
Follow-up 03,
….
so I need to get another variable that will have (numerical) value 1 if InstanceName=Follow-01, value 2 if InstanceName=Follow-up 02, 3 if InastanceName=Follow-up 03 etc, and this is easy to do using proc sql, and conditional statement “case when … then … else…end” or SAS data statement...
The problem is that Folllow-up xx number is changing, and within different data sets might be up 4, while within other data sets might be 15 and up.
So is it possible to make this “case when… then… else end” statement more data dependent, so I don’t always have to check how may Follow-up xx are there for each set of data, and that SAS runs this up the last Follow-up xx available?


Here how the code, in general, look like:

proc sql;
create table Table_1 as
select Var1, Var2, case
when InstanceName="Follow-up 01" then 1
when InstanceName="Follow-up 02" then 2
when InstanceName="Follow-up 03" then 3
….
else ...
end as FUP_NUM
from Data_1;
quit;

 

Thank you very much for your time,
Batta


 

Batta
Obsidian | Level 7

Great, thanks! 

My apology for late response, a lot of other things came up to be done in the meantime 🙂

 

Kind regards,

Batta 

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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
  • 2 replies
  • 657 views
  • 3 likes
  • 2 in conversation