SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Question to include value '0' as prefix for the field values in Data Management Studio 2.4

Reply
Contributor
Posts: 21

Question to include value '0' as prefix for the field values in Data Management Studio 2.4

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

Super User
Posts: 10,023

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

How about assign a format to it ?

format cocd z4.;

Contributor
Posts: 21

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

Hi

Smiley Happy

Respected Advisor
Posts: 3,156

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

you don't need 'if' condition, just

cocd_text = put(cocd, z4.);

Super User
Posts: 19,789

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

That isn't SAS code or straight SQL either.

use the the put function with z4 format.

cocd_text = put(cocd, z4.);

Super User
Posts: 11,343

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

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.

SAS Employee
Posts: 85

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

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

Contributor
Posts: 21

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

Dear Skillman,

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

Regards,

Shaheen

Super User
Posts: 7,771

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

Pardon me, but what type of code is that? Looks like no language I ever saw.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
SAS Employee
Posts: 85

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

Posted in reply to KurtBremser

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

Super User
Super User
Posts: 7,039

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

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

SAS Employee
Posts: 75

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

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

Occasional Contributor
Posts: 6

Re: Question to include value '0' as prefix for the field values in Data Management Studio 2.4

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,

Ask a Question
Discussion stats
  • 12 replies
  • 642 views
  • 4 likes
  • 10 in conversation