BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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;
1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

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;

Capture d’écran 2020-03-14 à 17.17.08.png

Best,

View solution in original post

5 REPLIES 5
Reeza
Super User
Read it in as a number and use the Z4 format which will add zeros.

results_padded = put(input(result, 8.), z4.);

ybz12003
Rhodochrosite | Level 12

The code change 78790 to 79E3.  Not the one I want.

mkeintz
PROC Star

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&lt;- 99999=[z5.]
 999999&lt;-999999=[z6.]  ;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ed_sas_member
Meteorite | Level 14

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;

Capture d’écran 2020-03-14 à 17.17.08.png

Best,

Tom
Super User Tom
Super User

@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'.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1005 views
  • 1 like
  • 5 in conversation