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
How about assign a format to it ?
format cocd z4.;
Hi ballardw,
I cannot change the input of file hence trying other options. I have received answer for the above question. Thank you.
Regards,
Shaheen
Hi Reeza, Hai Kuo, xia keshan,
I am new to this and not sure of what z4 format is. However i have received answer to my question. Thank you.
Regards,
Shaheen
you don't need 'if' condition, just
cocd_text = put(cocd, z4.);
That isn't SAS code or straight SQL either.
use the the put function with z4 format.
cocd_text = put(cocd, z4.);
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.
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
Dear Skillman,
Thank you for your codes. It is working fine.
Regards,
Shaheen
Pardon me, but what type of code is that? Looks like no language I ever saw.
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
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
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
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:
ID | CODE |
111 | 20 |
112 | 40 |
113 | 20 |
114 | 40 |
115 | CMR2 |
116 | CMR3 |
117 | 20 |
120 | 20 |
121 | 20 |
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:
ID | CODE | CODE_4_ZEROS | CODE_11_ZEROS |
111 | 20 | 0020 | 00000000020 |
112 | 40 | 0040 | 00000000040 |
113 | 20 | 0020 | 00000000020 |
114 | 40 | 0040 | 00000000040 |
115 | CMR2 | CMR2 | 0000000CMR2 |
116 | CMR3 | CMR3 | 0000000CMR3 |
117 | 20 | 0020 | 00000000020 |
120 | 20 | 0020 | 00000000020 |
121 | 20 | 0020 | 00000000020 |
Regards,
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.