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;
Code | Codeall |
001-002 | 1 |
001-002 | 2 |
0030 | 30 |
0031 | 31 |
00320 | 320 |
00321 | 321 |
00322 | 322 |
00323 | 323 |
00324 | 324 |
00329 | 329 |
004-009 | 4 |
004-009 | 5 |
004-009 | 6 |
004-009 | 7 |
004-009 | 8 |
004-009 | 9 |
007 | 7 |
0081-0087 | 81 |
0081-0087 | 82 |
0081-0087 | 83 |
0081-0087 | 84 |
0081-0087 | 85 |
0081-0087 | 86 |
0081-0087 | 87 |
01214-01215 | 1214 |
01214-01215 | 1215 |
01402 | 1402 |
0202 | 202 |
0203-0205 | 203 |
0203-0205 | 204 |
0203-0205 | 205 |
There's an ambiguity there:
7 007
7 004-009
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-002 | 1 IN THIS YOUR ARE EXTRACTION 1 |
001-002 | 2 IN THIS YOU ARE EXTRACTION IS 2 |
Why do feel the need to convert the variable from character to numeric?
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
Brilliant thank you EJ!!
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.
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.