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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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