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

suppose i have a column named number and it has a value like 6458903 and i want this value in target table as 6500000

and its length is not fixed how we can do this.??

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You would use the round function.  However you would first take the lengthn(put(number,best.)), then remove 2 from that and then use that to create your rounding amount.  Something like:

data want;
  orig=6458903;
  num=lengthn(strip(put(orig,best.)))-2;
  roundto=cats("1",repeat("0",num-1));
  new=round(orig,roundto);
run;

Note I have specifically left all the working out and made it verbose to show the process, you can shrink it down.

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You would use the round function.  However you would first take the lengthn(put(number,best.)), then remove 2 from that and then use that to create your rounding amount.  Something like:

data want;
  orig=6458903;
  num=lengthn(strip(put(orig,best.)))-2;
  roundto=cats("1",repeat("0",num-1));
  new=round(orig,roundto);
run;

Note I have specifically left all the working out and made it verbose to show the process, you can shrink it down.

Son_Of_Krypton
Fluorite | Level 6
Thanks for the solution '_'
data_null__
Jade | Level 19
146  data _null_;
147     do orig=6458903,670,456123,32,645890,64589,6458,645,64,6;
148        l = ceil(log10(orig));
149        u = 10**(l-2);
150        round = round(orig,U);
151        put (_all_)(=);
152        end;
153     run;

orig=6458903 l=7 u=100000 round=6500000
orig=670 l=3 u=10 round=670
orig=456123 l=6 u=10000 round=460000
orig=32 l=2 u=1 round=32
orig=645890 l=6 u=10000 round=650000
orig=64589 l=5 u=1000 round=65000
orig=6458 l=4 u=100 round=6500
orig=645 l=3 u=10 round=650
orig=64 l=2 u=1 round=64
orig=6 l=1 u=0.1 round=6

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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