SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

how to do it with arrays

Reply
Frequent Contributor
Posts: 89

how to do it with arrays

Hi frnds...
I have a problem that could be a simple one.
In a dataset I have two variables id and spend
like
data a;
input id spend;
cards;
1 100
1 200
1 300
2 150
2 690
;
run;

I want output like

id jan feb mar
1 100 200 300
2 150 690 .

through arrays

PROC Star
Posts: 7,362

Re: how to do it with arrays

already answered this exact same question earlier this morning:

data have;

input id spend;

cards;

1 100

1 200

1 300

2 150

2 690

;

run;

data want;

set have;

by id;

array x{*} jan feb mar;

retain jan feb mar     ;

if first.id then do;n=0;call missing(of x{*});end;

n+1;

x{n}=spend;

if last.id;

drop n spend;

run;

Respected Advisor
Posts: 3,777

Re: how to do it with arrays

This is a good problem to show how we can apply DO UNTIL(LAST DOT to "simplify" the code.

data have;
   input id spend;
   cards;
1 100
1 200
1 300
2 150
2 690
;;;;
   run;
data m(drop=spend);
   do _n_ = 1 by 1 until(last.id);
      set have;
      by id;
      array m[3] jan feb mar;
      m[_n_] = spend;
     
end;
  
run;
proc print;
  
run;
Ask a Question
Discussion stats
  • 2 replies
  • 234 views
  • 1 like
  • 3 in conversation