BookmarkSubscribeRSS Feed
Shah
Obsidian | Level 7

I have input field name as COCD which contains values and length of value is upto 4 digits. I require the output to be in 4 digits for all the values.  It means that if the COCD = 1 then the output should be changed to 0001. Is there any function to perform these type of changes?

I have tried the below expression but didn’t get the correct result :

if len(COCD) == 1 then cocd_text = left(000,Converstion_COCD)

COCD

Desired Output

5

0005

8

0008

33

0033

44

0044

555

0555

6666

6666

Regards,

Shaheen

12 REPLIES 12
Ksharp
Super User

How about assign a format to it ?

format cocd z4.;

Shah
Obsidian | Level 7

Hi

Smiley Happy

Haikuo
Onyx | Level 15

you don't need 'if' condition, just

cocd_text = put(cocd, z4.);

Reeza
Super User

That isn't SAS code or straight SQL either.

use the the put function with z4 format.

cocd_text = put(cocd, z4.);

ballardw
Super User

One would also ask if these values started with leading zeroes and they disappeared when brought into SAS. If that is the case I would look at my input to see if that could be corrected there, unless of course you are doing arithmetic with COCD.

skillman
SAS Employee

This expression will give you what you want:

/* Pre-processing Expression*/

string desired_output

hidden integer i

desired_output = ''

i = 0

/*Expression*/

if len(`COCD`) < 4

  begin

  desired_output = `COCD`

  while i <  (4 - len(`COCD`))

  begin

  desired_output = '0' & desired_output

  i = i + 1

  end

  end

else

  desired_output = `COCD`

  i = 0

dm_expression.png

Shah
Obsidian | Level 7

Dear Skillman,

Thank you for your codes. It is working fine. Smiley Happy

Regards,

Shaheen

skillman
SAS Employee

It is a SAS Data Management Expression code. The original poster asked for help within SAS Data Management Studio, and this was exactly what he wanted.

-shawn

Tom
Super User Tom
Super User

That logic seems way too complex.  I have never programmed in that language, but the code could be much simpler.

string desired_output

desired_output = `COCD`

while len(desired_output) < 4

  begin

    desired_output = '0' & desired_output

  end

DaveR_SAS
SAS Employee

If you would like to know more about the expression provided by skillman, see the DataFlux expression language reference: http://support.sas.com/documentation/onlinedoc/dfdmstudio/2.5/dfU_ELRG.pdf

ricardo_benatti
Fluorite | Level 6

hi ....

to evaluate the group ...

    could be a solution, simpler,

    receive in a variable, the concatenation of the "N" ZEROS characters, right, with the content attribute:

INPUT:

IDCODE
11120
11240
11320
11440
115CMR2
116CMR3
11720
12020
12120

EEL >> PRE PROCESSING

   string(11) W_2_ZEROS_LEN_11

   string(04) W_2_ZEROS_LEN_04

EEL >> PRE PROCESSING

   W_2_ZEROS_LEN_04 = right( "0000" & trim( `CODE` ), 04 )

   W_2_ZEROS_LEN_11 = right( "00000000000" & trim( `CODE` ), 11 )

OUTUPUT:

IDCODECODE_4_ZEROSCODE_11_ZEROS
11120002000000000020
11240004000000000040
11320002000000000020
11440004000000000040
115CMR2CMR20000000CMR2
116CMR3CMR30000000CMR3
11720002000000000020
12020002000000000020
12120002000000000020

Regards,

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 12 replies
  • 2306 views
  • 4 likes
  • 10 in conversation