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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 361 views
  • 3 likes
  • 2 in conversation