Hello,
I have a sample test dataset. I would like to put 0 in front of the digit shown the result column.
data test;
infile datalines dsd;
input Name : $300. Result : $200. ;
datalines;
01, 0001
4000, 4000,
78790, 78790,
2, 0002,
111, 0111
;
run;
Hi @ybz12003
You can use the following approach to achieve this:
data want;
set test;
length result2 $ 200;
if input(Name, 8.) < 1000 then result2 = put(input(Name, 8.), z4.);
else result2 = Name;
run;
Best,
The code change 78790 to 79E3. Not the one I want.
You can make your own format:
proc format;
picture myfmt
0 - 9999='9999'
99999<- 99999='99999'
999999<- 999999='999999';
run;
data x;
input x;
format x myfmt. ;
format x mfmt. ;
datalines;
01
4000
78790
2
111
;
proc print;run;
Edited change. The PICTURE statement above replaced this VALUE statement:
value myfmt 0 - 9999=[z4.] 99999<- 99999=[z5.] 999999<-999999=[z6.] ;
Hi @ybz12003
You can use the following approach to achieve this:
data want;
set test;
length result2 $ 200;
if input(Name, 8.) < 1000 then result2 = put(input(Name, 8.), z4.);
else result2 = Name;
run;
Best,
@ybz12003 wrote:
Hello,
I have a sample test dataset. I would like to put 0 in front of the digit shown the result column.
data test; infile datalines dsd; input Name : $300. Result : $200. ; datalines; 01, 0001 4000, 4000, 78790, 78790, 2, 0002, 111, 0111 ; run;
Do you actually have the 8 leading spaces in the values of NAME implied by starting your lines of data in column 9 instead of column 1? If so then using the $ informat instead of the $CHAR informat will eliminate the leading spaces. Worse if those lines of data start with a tab character you might end up with the actual tab character in the data if you are using SAS/Studio instead of Display Manager to edit and submit your SAS code.
Assuming the values of NAME do not have either leading spaces, embedded space (or tabs), then you could just right align the values and replace the leading spaces with zeros. Adjust the length of the right aligned value to have a minimum of 4 characters.
result = translate(putc(name,cats('$',max(4,length(name)),'.-R')),'0',' ');
Note this will convert a missing value (all blank value) into '0000'.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.