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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
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-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!

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.

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