Character alignment

Reply
Occasional Contributor
Posts: 17

Character alignment

Hi everyone!

I have a query buddies, so need your suggestions.

here goes the problem,

I have a character variables ' x ' and has an observations:

1.1

1.111

111.11

123.341

1

231.110

now the above observation i need in the following way:

  1.1

  1.111

111.11

123.341

    1

231.110

So how well i do this?

since it is a character type i used scan function and seperated two part like before and after '.' but not able to concatenate it as requried.

Super Contributor
Posts: 336

Re: Character alignment

I don't get what you are trying to do. Adding spaces depending on .. which rule?

Occasional Contributor
Posts: 17

Re: Character alignment

i just want the values of x variable to appear like

   1.1

   1.111

111.11

123.341

   1

231.110

Super User
Super User
Posts: 7,413

Re: Character alignment

Something like:

data test;

  a=1.1; output;

  a=1.111; output;

  a=111.11; output;

run;

data test;

  set test;

  length b $20.;

  b=tranwrd(put(a,z12.3),"0"," ");

run;

Occasional Contributor
Posts: 17

Re: Character alignment

i dont want to append '0' for the obs

Super User
Super User
Posts: 7,413

Re: Character alignment

Did you try running the code.  As I stated, the put to format z will pad the numeric out, but as you don't want the zeros then it goes to a character field where a tranwrd is operated on it to get rid of the 0's.

Occasional Contributor
Posts: 17

Re: Character alignment

Yup! i did that, it's throwing an error that the format z cannot be loaded

Respected Advisor
Posts: 3,777

Re: Character alignment

Look again I expect the error is $Z cannot be loaded.  program uses numeric data yours character.

Occasional Contributor
Posts: 17

Re: Character alignment

yes its $z

Super Contributor
Posts: 336

Re: Character alignment

If you try this in data _null_'s data step?

Invar_Test=Repeat(' ',Maxi-Length(Compress(Scan(Invar,1,'.'))))!!Compress(Invar);

Super User
Posts: 6,962

Re: Character alignment

That's what my code does, exactly as you wanted in your first post.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 17

Re: Character alignment

Nope! i didn't get the excepted output. Its varying with number of character before '.' 

Super User
Posts: 6,962

Re: Character alignment

Your suggested result from the OP (cut&paste) looks like that:

  1.1      (2 leading blanks)

  1.111   (2 leading blanks)

111.11    (0 leading blanks)

123.341 (0 leading blanks)

    1       (4 leading blanks)

231.110  (0 leading blanks)

This is EXACTLY what a proc print of my result into ODS LISTING will show you, buddy.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 17

Re: Character alignment

Ya, even i got the exact out put but this is not my excepted output, friend. Dot in all the value should be in  the same column (straight vertical line) 

Super User
Posts: 6,962

Re: Character alignment

Then you only need this slight variation:

data want;

set have;

length newvar $ 7;

if indexc(left(invar),'.') = 0

then newvar = put(input(invar,7.),3.);

else substr(newvar,5-indexc(left(invar),'.')) = left(invar);

run;

And next time, take a look at your wanted results before posting.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 29 replies
  • 619 views
  • 3 likes
  • 9 in conversation