BookmarkSubscribeRSS Feed
lisahoward
Calcite | Level 5

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
6 REPLIES 6
TarunKumar
Pyrite | Level 9

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
data_null__
Jade | Level 19
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;
Tom
Super User Tom
Super User

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

esjackso
Quartz | Level 8

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

lisahoward
Calcite | Level 5

Brilliant thank you EJ!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2235 views
  • 1 like
  • 6 in conversation