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

Dear all,

I have a SAS data set(mydata.sas7bdat). It looks like the following:

Field1

24

3872

349

1111

If I use text function in Excel, I can convert the numbers to character values using =text(A2, "0000").

Field2

0024

3872

0349

1111

How can I do that in SAS?


1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

Not sure if this is what you want:

data have;

input Field1;

cards;

24

3872

349

1111

;

data want (keep=_c rename=_c=Field1);

set have;

_c=put(field1,z4.);

run;

proc print;run;

Regards,

Haikuo

View solution in original post

6 REPLIES 6
Peter_C
Rhodochrosite | Level 12

ODS tagsets.excelxp

provides a way to do this

It allows you to define the custom format for excel to apply the format

It also allows you to apply an excel formula

There are many examples in the ODS forum

Haikuo
Onyx | Level 15

Not sure if this is what you want:

data have;

input Field1;

cards;

24

3872

349

1111

;

data want (keep=_c rename=_c=Field1);

set have;

_c=put(field1,z4.);

run;

proc print;run;

Regards,

Haikuo

tesu
Calcite | Level 5

Thank you, Haikuo. It works!

Linlin
Lapis Lazuli | Level 10

or

data have;

input Field1;

cards;

24

3872

349

1111

;

proc sql;

  select put(field1,z4.) as field1

    from have;

quit;

kuridisanjeev
Quartz | Level 8

data have;

input Field1;

cards;

24

3872

349

1111

;

run;

data have1;

set have;

if length(field1)=1 then "000"||field1;

else;

if length(field1)=2 then "00"||field1;

else;

if length(field1)=3 then "0"||field1;

else field1=field1;

run;

Regards

Sanjeev K

Peter_C
Rhodochrosite | Level 12

SAnjeev

YOu have to learn about the Z. format. The example a few postings earlier in this thread show how much it improves on your solution.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 3103 views
  • 0 likes
  • 5 in conversation