SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
jenim514
Pyrite | Level 9

Hi,

 

I have a dataset with multiple timepoints in each observation (eg.  lbtim1 lbtim2 lbtim3...).  They are all in time5. numeric format.

How can I change all of these to character format (TOD5.)?

 

All of the time variables have the same prefix 'lbtim'  with the timepoint.  Here is what I started with:

 


data prsupp;
set db.lbi;

array timn (*) lbitim:;
array timc (*) lbtimc:;
do i= 1 to dim (timn);
if ~missing (timn(i)) then timc(i)=put(timn(i),tod5.);
end;
run;

 

 

I guess it's something with the new character variables I'm trying to create.  Or can I just overwrite the numeric variables to character somehow?

 

Help is appreciated!

1 REPLY 1
ballardw
Super User

Are you sure you need a character variable? Often using the desired format at the appropriate time is sufficient.

 

The problem is that you created a numeric variable with your array statement and likely incorrectly if the variables are supposed to be new.

Do the lbtimc: variables already exist? If not the following doesn't work because it will create an array of 0 elements that would be numeric. I expect that your log showed something like:

WARNING: Defining an array with zero elements.

after the line with the line array timc (*) lbtimc:;

 

SAS cannot read your mind to know that you want another variable to match a list of existing variables.

Use

array lbtimc (5) $6 ;

to create 5 new variables lbtimc1-5 of length 6 characters. Use the number that matches your data.

If you leave out the 6 the variables will default to 8 characters

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 1 reply
  • 518 views
  • 0 likes
  • 2 in conversation