BookmarkSubscribeRSS Feed
bhca60
Quartz | Level 8

I want to format the variable plan.  Right now it's $24 CHAR but the table it joins to later after this step has plan as $15. CHAR:


data table2 (keep=state plan);
rename state_r = state ptypcd=plan;

set table1;
run;

2 REPLIES 2
ballardw
Super User

Have you checked to see if there is any value past the 15th character?

If so, you may need to make some decisions about what text value to keep.

 

The technique is simple: add a length statement to that data step before the Set statement:

 

data table2 (keep=state plan);
rename state_r = state ptypcd=plan;
length <variable> <new length i.e. $ 15>;
set table1;
run;

However your rename means that the variable name would be the OLD one so it modifies the existing variable what the values are brought in.

Tom
Super User Tom
Super User

You are using the verb  "format" ambiguously.  In SAS a format is something that determines how to display a value as text.  You can attach different formats to a variable and have the values display differently.

 

I suspect you want to change the LENGTH of a character variable.  In that case you should also take care that the variable does not have a format attached to it that will impact how it is displayed.

 

If you want to change it to be length 15 from length 24 then you need to worry about lose of data.  It might be safest to make a new variable and also check the current values before copying them to the new variable to make sure they will fit.

data table2 ;
  set table1;
  length plan $15  ;
  state=state_r ;
  plan=ptypcd ;
  if length(ptypcd) > 15 then put 'NOTE: Data will be truncated. ' _n_= ptypcd= plan=;
  keep state plan;
run;

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 821 views
  • 0 likes
  • 3 in conversation