BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jerry898969
Pyrite | Level 9

Hello,

 

I have a situation where I have data like this

X_1
X_2
X_3
X_4
X_5
Z_1
Z_2
Z_3
Z_4
Z_5
Z_6
Z_7
Z_8
Z_9
Z_10
Z_11

Where the data has a value over 9 I need 1-9 to have a leading zero. Based on the above data I need this to happen

X_1
X_2
X_3
X_4
X_5
Z_01
Z_02
Z_03
Z_04
Z_05
Z_06
Z_07
Z_08
Z_09
Z_10
Z_11

Here is the code I'm starting to work with.  This column of data can have values with no '_' or and '_' with characters after.  I only want to do this for values where there are numbers after the '_'.

data x ;
	set temp ;
	start = scan(val, 1, '_') ;
	end = scan(val, 2, '_') ;
	if end ne '' and ANYALPHA(end) = 0 then output ;
run ;

Thank you for any help you can give me.

 

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

You have to find the W(idth) for each VAL group X and Z.  Then you can creat the VAL with the proper number of leading zeros.

 

data val;
   input val:$8.;
   cards;
X_1
X_4
X_5
Z_1
Z_2
Z_6
Z_7
Z_8
Z_9
Z_10
Z_11
X_2
X_3
Z_3
Z_4
Z_5
Q_1
Q_200
;;;;
   run;
data valv / view=valv;
   set val;
   length valgroup $32;
   valgroup = scan(val,1,'_');
   valnum   = input(scan(val,-1,'_'),8.);
   run;
proc sort data=valv out=val2;
   by valgroup valnum;
   run;
proc print;
   run;
proc summary data=val2;
   by valgroup;
   output out=max(drop=_:) max(valnum)=max;
   run;
data val3;
   merge val2 max;
   by valgroup;
   w = floor(log10(max)+1);
   length new_val $10;
   new_val = catx('_',valgroup,putn(valnum,'Z',w));
   run;
proc print;
   run;

 

Capture.PNG 

View solution in original post

4 REPLIES 4
data_null__
Jade | Level 19

You have to find the W(idth) for each VAL group X and Z.  Then you can creat the VAL with the proper number of leading zeros.

 

data val;
   input val:$8.;
   cards;
X_1
X_4
X_5
Z_1
Z_2
Z_6
Z_7
Z_8
Z_9
Z_10
Z_11
X_2
X_3
Z_3
Z_4
Z_5
Q_1
Q_200
;;;;
   run;
data valv / view=valv;
   set val;
   length valgroup $32;
   valgroup = scan(val,1,'_');
   valnum   = input(scan(val,-1,'_'),8.);
   run;
proc sort data=valv out=val2;
   by valgroup valnum;
   run;
proc print;
   run;
proc summary data=val2;
   by valgroup;
   output out=max(drop=_:) max(valnum)=max;
   run;
data val3;
   merge val2 max;
   by valgroup;
   w = floor(log10(max)+1);
   length new_val $10;
   new_val = catx('_',valgroup,putn(valnum,'Z',w));
   run;
proc print;
   run;

 

Capture.PNG 

slchen
Lapis Lazuli | Level 10

data have;
input x $;
cards;
X_1
X_2
X_3
X_4
X_5
Z_1
Z_2
Z_3
Z_4
Z_5
Z_6
Z_7
Z_8
Z_9
Z_10
Z_11
;

data want;
set have;
x=ifc(index(x,'Z')>0, catx('_',scan(x,1,'_'),put(input(scan(x,2,'_'),2.),z2.)),x);
run;

ChrisHemedinger
Community Manager

You've received some great options from @data_null__ and @slchen.  I don't have a better way, but I'll offer this: I think you should consider using leading 0s for ALL of your values, not just those that exceed a 1-digit length.  I think you'll find the consistency is better for sorting and reporting.  I don't know the context of your application here, but what if today you have X_1 - X_5, but tomorrow your Xs grow to X_11?  The consistency and predictability of the values might help you in the long run.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
jerry898969
Pyrite | Level 9
Thank you Chris. I'm going through the above solutions now. The reason I can't do that is that I will be joining them based on this value to another table that is coming from someone else and they only add the zero when the count is greater then 9. Thanks again. I'll reply back and accept the solution as soon as I get it resolved. Thank you

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 1792 views
  • 4 likes
  • 4 in conversation