BookmarkSubscribeRSS Feed
lillymaginta
Lapis Lazuli | Level 10

I have a numeric variable coded as 0/1

I want to create a character variable that would allow at least 11 characters in the word, can someone guide the best way to code this? 

Thanks,

data have;
input drug;
cards;
0
1  
0;  
Run; 

 I want to create a character variable such as 

if drug1=1 then ch= 'healthinsurance' 

 

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Not sure i understand you req, do you mean assigning length for your char?

 

 

data have;
input drug;
cards;
0
1
;
Run;

data want;
set have;
length ch $20;
if drug=1 then ch= 'healthinsurance' ;
run;

Tom
Super User Tom
Super User

The same way you would define the length for any variable in your data set. Use the LENGTH statement.

data want ;
  length drug 8 ch $12 ;
  input drug;
  if drug then ch= 'healthinsurance' ;
cards;
0
1
;

 

Kurt_Bremser
Super User

Define a format with your translations, and the resulting variable (when the format is used in a put() function) will have the appropriate length.

proc format;
value mytrans
  1 = 'healthinsurance'
  0 = 'other'
;
run;

data want;
input numvar;
charvar = pu(numvar,mytrans.);
cards;
0
1
;
run;
ballardw
Super User

@Kurt_Bremser wrote:

Define a format with your translations, and the resulting variable (when the format is used in a put() function) will have the appropriate length.

proc format;
value mytrans
  1 = 'healthinsurance'
  0 = 'other'
;
run;

data want;
input numvar;
charvar = pu(numvar,mytrans.);
cards;
0
1
;
run;

Or just use the FORMAT to display the value without creating another variable.

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 25. 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
  • 4 replies
  • 696 views
  • 6 likes
  • 5 in conversation