Your character variable value is all digit? Use INPUT to read as a number, then Z4. format to add leading zeroes.
data test;
length charvar $ 4 padded $ 4;
infile datalines;
input charvar;
/* read charvar as number, then format with leading zeroes */
/* then put result into padded */
padded = put(input(charvar,best4.),z4.);
datalines;
28
1034
4
8759
1234
234
34
;
run;
Output:
charvar padded 28 0028 1034 1034 4 0004 8759 8759 1234 1234 234 0234 34 0034
Ksharp
Your character variable value is all digit? Use INPUT to read as a number, then Z4. format to add leading zeroes.
data test;
length charvar $ 4 padded $ 4;
infile datalines;
input charvar;
/* read charvar as number, then format with leading zeroes */
/* then put result into padded */
padded = put(input(charvar,best4.),z4.);
datalines;
28
1034
4
8759
1234
234
34
;
run;
Output:
charvar padded 28 0028 1034 1034 4 0004 8759 8759 1234 1234 234 0234 34 0034
Ksharp
Ksharp
In case I have a variable(character format) with both digits and characters, how will the code change?
For instance,
Raw data Desired output
007851 007851
05614 005614
2447 002447
Could you please help?
Hi
I just got the solution to my problem here:
https://groups.google.com/forum/#!topic/comp.soft-sys.sas/uBuLYXMRqhw
if length(raw data) = 6 then new=raw data;
else new=repeat('0',6-length(raw data)-1)||raw data;
Just thought would share the code here if anyone needs
Hi Namrata,
I believe your code will not produce a desired output if the input records with length more than 6.
can anyone suggests?
OK. Here are two solutions . Pick one up you like :
data have; input Raw $char6.; cards; 007851 05614 2447 ; run; /*** the first way ***/ data want1; set have; want=translate(right(raw),'0',' '); run; /*** the second way ***/ data want2; set have; want=put(input(raw,best32.),z6.); run;
Xia Keshan
I have a similar problem. My zip variable data has following values:
zip
123456789
123456789
1234
1234
12345
123456789
12345
12345
1234
123456789
I want to get a single variable with:
Thanking you in advance!
Found the answer to my question:
data test2; set ptcz2;
zip5 = substr(compress(zip),1,5);
zip5v2 = substr(("0000"||zip5),length(zip5),5);
run;
I have a similar problem. My data has following values:
zip
123456789
-123456789
1234
-.234
12345
-.23456789
12345
12345
1234
123456789
I want to get a single variable with:
Thank you
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: