BookmarkSubscribeRSS Feed
oht
Calcite | Level 5 oht
Calcite | Level 5

a Hi ,

For some reason, I got hung on the following simple logic to fill value to a fix lenght without truncation.

DATA TEST;
  RETAIN B ;
  RETAIN LENB 0;
  LENGTH B $10.;
   INPUT A $CHAR4.;

LENA=LENGTH(A);

  B=CATT(B,A);

LENB=LENGTH(B);

DIFF=10-LENB -1;

IF DIFF < 0 THEN DO;
    B=' ' ;
LENB=0;
  END;

DATALINES;
  aaaa
bb
ccc
dd
gggg
F

EE
     HHH 
;

What I am looking for is to fill the space B with A(including space) without truncation so the result should look like

  aaaa bb

ccc  dd

gggg F EE

     HHH

Any idea ? Thanks

3 REPLIES 3
DBailey
Lapis Lazuli | Level 10

I don't understand your desired output.  Is it that the output can't exceed length 10 so you're having to check to see if adding the next variable would cause it to exceed that length?

oht
Calcite | Level 5 oht
Calcite | Level 5

Yes. We want to fill the string B as much as possible without trunctuion. If the next string do not have enough space to fill in the entire value, then start a new B to continue. The problem of my logic is not working well.

Thanks for your help

DBailey
Lapis Lazuli | Level 10

data have;

length str $10.;

input str CHAR10.;

DATALINES;

  aaaa

bb

ccc

dd

gggg

F

EE

     HHH

;

run;

DATA want(keep=tmpStr rename=(tmpStr=Str));

set have end=eof;

length tmpStr $10.;

retain tmpStr '';

if length(tmpStr) + length(str) < 10 then do;

    tmpStr = trim(tmpstr)||' '||trim(str);

    end;

else do;

    output;

    tmpStr=str;

    end;

if eof then output;

run;

produces this

                                                          Obs       Str

                                                           1         aaaa

                                                           2     bb ccc dd

                                                           3     gggg F EE

                                                           4          HHH

:

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 770 views
  • 0 likes
  • 2 in conversation