BookmarkSubscribeRSS Feed
elli444
Calcite | Level 5

Hello all, 

I am creating a new variable in a data step. The code is below:

if hisp_not='.' then ethnicity1='';
if hisp_not='1' then ethnicity1='';
if hisp_not='0' then ethnicity1='E1';

 

When I run a proc print of var ethnicity1 the output has changed to "E". 

elli444_0-1593444321573.png

 

It is essential for the output data to be E1 in order to import into our database. 

I checked and the format is $4 so it's not a formatting issue. 

 

Does anyone have any idea why this would happen and how to fix it?

Thank you!

2 REPLIES 2
ballardw
Super User

If you examine the properties of the variable Ethnicity1 you will find that it has a length of one character and so cannot accept the second character. Format does not control length, only display appearance.

 

If the first use of a character variable is an assignment such as your Ethnicity1=''; that will set the length of the variable. You need to specify a length before use if want it to hold longer values.

 

length Ethnicity1 $ 2 ;

BEFORE the assignment code you are using.

If you want multiple values treated the same you can use the IN operator

if hisp_not='.' then ethnicity1='';
if hisp_not='1' then ethnicity1='';

/* can be replaced with*/
if hisp_not in ('.' '1' ) then ethnicity1='';

 

Reeza
Super User
is your variable hisp_not numeric or character?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 842 views
  • 1 like
  • 3 in conversation