Hello--
I recently received a data set with Var1 values: '1' '2' '3' '4' '5' '10' '11' '12' '13' '14' '15' 'unknown.' This variable is obviously character, but I need leading 0s on the digits that are less than 10. Is there an elegant way to do this?
Thanks
Hello @raivester,
I'd suggest this:
data want;
set have;
if lengthn(var1)=1 then var1='0'||var1;
run;
(I use LENGTHN, not LENGTH, because missing values should not be replaced by zeros.)
will this work if one of the variable values is the character string "unknown"?
Try this:
data have;
input var1 $;
datalines;
1
2
3
4
5
6
11
12
13
14
15
unknown
;
data want;
set have;
if anyalpha(var1) = 0
then var1 = put(input(var1,best.),z2. -l);
run;
Hello @raivester,
I'd suggest this:
data want;
set have;
if lengthn(var1)=1 then var1='0'||var1;
run;
(I use LENGTHN, not LENGTH, because missing values should not be replaced by zeros.)
Ahh! Good thinking. Thanks for the help
Do you expect to need to do arithmetic with these values? Or model with the numeric value ?
If so, then read them as numeric so the unknown are missing and if you need a leading zero use a Z2. format.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.