Help using Base SAS procedures

Leading Zeros

Reply
Contributor
Posts: 57

Leading Zeros

I have to convert a character variable to a numeric variable to be able to run the  datastep below:  However the variable I want to keep has lost all leading zeros.  How can i add these back onto the codeall variable.  As you can see there are different numbers of zeros for each code so I can't use the standard Zx format as for example codeall 329 should be  00329 but codeall 203 should be 0203. Many thanks for any help you can give me.  Is there anyway to substr the code zeros and concatenate them onto the codeall value?

data want ;

     set codes;

BeginCode = input(scan(Code,1),best10.);

EndCode = input(scan(Code,2),best10.);

if missing(EndCode) then do Codeall=input(Code,best10.);output ;end;

  else

do Codeall = BeginCode to Endcode;

          output ;

     end;

run;

CodeCodeall
001-0021
001-0022
003030
003131
00320320
00321321
00322322
00323323
00324324
00329329
004-0094
004-0095
004-0096
004-0097
004-0098
004-0099
0077
0081-008781
0081-008782
0081-008783
0081-008784
0081-008785
0081-008786
0081-008787
01214-012151214
01214-012151215
014021402
0202202
0203-0205203
0203-0205204
0203-0205205
Super User
Posts: 6,926

Re: Leading Zeros

There's an ambiguity there:

7 007

7 004-009

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 74

Re: Leading Zeros

pls eleobrate. while exporting data in excel leeading zero are missing or at  the time of the conversion and on what logic you are extracting the data.

001-0021 IN THIS YOUR ARE EXTRACTION 1

001-0022 IN THIS YOU  ARE EXTRACTION IS 2
Respected Advisor
Posts: 3,777

Re: Leading Zeros

data zero;
   input string $20.;
  
length w1-w2 $20;
   w1=scan(string,
1,'-');
   w2=scan(string,-1,'-');
   z = verify(w1,'0')-1;
   n1=input(w1,
f8.);
   n2=input(w2,f8.);
   length zeros $20;
  
do n = n1 to n2;
      zeros = cats(substrn('00000000',1,z),n);
      output;
     
end;
  
cards;
001-002
887
999
0030
0031
00320
000321
004-009
4-9
007
0081-0087
01214-01215
0203-0205
;;;;
   run;
proc print;
  
run;
Super User
Super User
Posts: 6,495

Re: Leading Zeros

Why do feel the need to convert the variable from character to numeric?

Super Contributor
Posts: 333

Re: Leading Zeros

This is connected to the lisahoward other thread...

Is this going to be used as a lookup table for some other procedure? If so the codeall variable needs to be in the same variable type (character / numeric) as the variable being compared too.

The problem is to actually retain leading zeros in the data in SAS is to have the variable as a character string. The format really only changes how the variable is displayed and not how it is used in matches / merges.

here is some simple code to convert to what you want ... just make sure that is what you really want.

data have;

  length code $ 10;

  input code $;

  cards;

001-002

0030

0031

00320

00321

00322

00323

00324

00329

004-009

01402

0202

0203-0205

run;

data want ;

  drop i;

  length codeall $ 20 int $ 5;

     set have;

BeginCode = input(scan(Code,1),best10.);

EndCode = input(scan(Code,2),best10.);

if missing(EndCode) then do;

  Codeall=code;

  output;

;end;

  else do;

  if code=: "0000" then int = "0000";

  else if code=: "000" then int = "000";

  else if code=: "00" then int = "00";

  else if code=: "0" then int = "0";

  do i = BeginCode to Endcode;

  Codeall = cats(int,i);

          output ;

  end;

    end;

run;

Hope this helps!

EJ

Contributor
Posts: 57

Re: Leading Zeros

Brilliant thank you EJ!!

Ask a Question
Discussion stats
  • 6 replies
  • 861 views
  • 1 like
  • 6 in conversation