DATA Step, Macro, Functions and more

Keeping Nth Column

Reply
Contributor
Posts: 62

Keeping Nth Column

I have a dataset that looks like this:

CBG tru1-tru100.

where cbg is a unique identifier tru is an array with a label that identifies its monthly date.

i want to pull the data together into quarters, so i want every 3rd column for tru1-tru100.

Any Ideas?

i was thinking about transposing and then using the mod function.

Trusted Advisor
Posts: 1,301

Re: Keeping Nth Column

data foo;

length cbg tru1-tru100 8;

run;

proc sql noprint;

select name into :names separated by ' '

   from dictionary.columns

  where libname='WORK'

        and memname='FOO'

  and substr(name,1,3)='tru'

  and mod(input(substr(name,4),3.),3)=0;

quit;

data bar;

set foo;

keep &names;

run;

Super Contributor
Posts: 1,636

Re: Keeping Nth Column

data have;

id =22;

array q(*) q1-q30;

do _n_=1 to dim(q);

  q(_n_)=1; end;

run;

%macro test;

data want;

  set have;

  keep id %do i=3 %to 30 %by 3;

    q&i %end;;

run;

%mend;

%test

proc contents data=want;run;

Contributor
Posts: 62

Re: Keeping Nth Column

Thanks FriedEgg & LinLin.

@LinLin I never knew that you could put a do loop in a keep statement!

Respected Advisor
Posts: 3,156

Re: Keeping Nth Column

Hi Jeffrey,

What LinLin has used is not a regular 'do loop', instead, it was a Macro loop, which, for one, you can only use it inside a macro; for two, you can literally put it any where inside macro. Its purpose is nothing but to render a 'text string'.

Regards,

Haikuo

Ask a Question
Discussion stats
  • 4 replies
  • 355 views
  • 6 likes
  • 4 in conversation