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

I am new to SAS and want to know if there is a way to do the following.

Import the excel file, and import all the variables. One variable is numeric, want to prefix a text to it as below. Can the code be simplified?

 

Proc sql; 

create table tt (drop = patientid) as
select *, cat("patient ",patientid) as patientid1 from Temp ;


create table tt2 (drop = patientid1) as
select *, patientid1 as patientid from tt;

quit;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Try using the RENAME= dataset option.

create table tt2(drop=num_patientid) as
  select *
       , cat('patient ',num_patientid) as patientid
  from TEMP(rename=(patientid=num_patientid))
;

But why are you using SQL for this?  It would be much clearer in normal SAS code instead.

data tt2;
  set TEMP;
  char_patientid=cat('patient ',patientid);
  drop patientid;
  rename char_patientid=patientid;
run;

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

What is the purpose of creating data set TT2? It seems you have everything you need in data set TT1. Please explain further.

--
Paige Miller
Hali2
Calcite | Level 5

I want to retain the same variable name patientid, since i dont know how to retain in tt1, created tt2. 

PaigeMiller
Diamond | Level 26

@Hali2 wrote:

I want to retain the same variable name patientid, since i dont know how to retain in tt1, created tt2. 


You wouldn't first DROP patientID and then in another SQL retain it. This should work

 

proc sql; 
     create table tt as
     select *, cat("patient ",patientid) as patientid1 from Temp;
quit;

 

 

I question the value of adding text in front of a numeric variable like Patient ID. Seems unnecessary to me.

 

--
Paige Miller
Tom
Super User Tom
Super User

Try using the RENAME= dataset option.

create table tt2(drop=num_patientid) as
  select *
       , cat('patient ',num_patientid) as patientid
  from TEMP(rename=(patientid=num_patientid))
;

But why are you using SQL for this?  It would be much clearer in normal SAS code instead.

data tt2;
  set TEMP;
  char_patientid=cat('patient ',patientid);
  drop patientid;
  rename char_patientid=patientid;
run;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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