BookmarkSubscribeRSS Feed
kdm
Calcite | Level 5 kdm
Calcite | Level 5

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.

7 REPLIES 7
Doc_Duke
Rhodochrosite | Level 12

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.

kdm
Calcite | Level 5 kdm
Calcite | Level 5

Hi Doc,

Could you provide me with an example.

Thanks,

Kurian

kdm
Calcite | Level 5 kdm
Calcite | Level 5

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?

Linlin
Lapis Lazuli | Level 10

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

Ksharp
Super User

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

kdm
Calcite | Level 5 kdm
Calcite | Level 5

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

Kurian

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
  • 7 replies
  • 4896 views
  • 0 likes
  • 4 in conversation