DATA Step, Macro, Functions and more

Character variable with dynamic length

Reply
Occasional Contributor kdm
Occasional Contributor
Posts: 11

Character variable with dynamic length

In the discussion

Combining the same variable on different records into a single variable

you all had given some wonderful suggestions. i went ahead and used LinLin's suggestion. I had the following requirement added.


I had one additional question. Can we have newline to be dynamic?


As in if we do not know the maximum number of rows that can have the same id. Does SAS support dynamic variables.


We have scenarios where newline can be 100, 350 or 500 and could be even more.

Trusted Advisor
Posts: 2,113

Re: Character variable with dynamic length

SAS does not support dynamic variable lengths.  You can obtain the same result as dynamic variables by passing the data once to obtain the maximum number of repeats for the newline variable and then using macro programming to set the length for the subsequent step.  The downside is that you have to read the data twice.

Occasional Contributor kdm
Occasional Contributor
Posts: 11

Re: Character variable with dynamic length

Hi Doc,

Could you provide me with an example.

Thanks,

Kurian

Occasional Contributor kdm
Occasional Contributor
Posts: 11

Re: Character variable with dynamic length

Hi,

I can creates a dataset with id as the variable , then I can do a count on the maximum occurrence of an id. Once I get this count can I declare the length variable with this count.

For example the max occurrence of an id is 5 and this is stored in countid. Suppose the lenth of the line variable is 50 then new line will be

length newline $countid;

Is this feasible?

Super Contributor
Posts: 1,636

Re: Character variable with dynamic length

then the length should be 5*50=250;

you can try something like the code below:

data have;

input id line$ 50.;

cards;

100  abcdefghijkollldsdksjd

100 djsjdklsjdksdlsldklskdsld

100 djskdjsldjlsdjlsdjlsdljskdjskdjskdioeioeoeoe

100 jksldksldklsldklsdksldksldklskdls

100 djsldksldksldklsdkdadskldkasldklsdksdklskdsk

200 djksldklskdlskdls

200 uuuuuuuuuuuuuuuuuu

;

proc sql noprint;

   select max(count)*50 into :countid separated by ' ' from (select count(id) as count from have group by id);

quit;

data want(keep=id newline);

length newline $&countid;

retain newline;

set have;

by id ;

newline=cats(newline,line);

if last.id then do;output ;

call missing(newline); end;

run;

proc contents;run;

Message was edited by: Linlin

Super User
Posts: 9,687

Re: Character variable with dynamic length

You can set it with the maximum length .

length newline $ 32767 ;

But if the length can't still hold all of the value, then I think you need two or more newline to hold .

Ksharp

Occasional Contributor kdm
Occasional Contributor
Posts: 11

Re: Character variable with dynamic length

What if the countid in linlin's example exceeds 32767, will the record get truncated?

Kurian

Super User
Posts: 9,687

Re: Character variable with dynamic length

YES.

Ask a Question
Discussion stats
  • 7 replies
  • 886 views
  • 0 likes
  • 4 in conversation