Changing data dimensions

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

Changing data dimensions

[ Edited ]

Hi to all,

 

Using only array statements or a call execute statement, is it possible to do this :

 

data have;
input a1-a10;
cards;
.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21
;
data want;
input a1-a7;
cards;
.101 .4    .7   .10  .13   .16  .19
.201 .5    .8   .11  .14   .17  .20
.3   .6    .9   .12  .15   .18  .21
;run;

 

Regards


Accepted Solutions
Solution
‎08-19-2017 02:03 PM
PROC Star
Posts: 7,531

Re: DataM

Here is another way using arrays:

data have;
  input a1-a21;
  cards;
.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21
;

data want (drop=_:);
  set have (rename=(a1-a21=_a1-_a21));
  array all(*) _a1-_a21;
  array part(7) a1-a7;
  do _i=1 to 3;
    _k=0;
    do _j=_i to dim(all) by 3;
      _k+1;
      part(_k)=all(_j);
    end;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,973

Re: DataM


DoumbiaS wrote:

Hi to all,

 

Using only array statements or a call execute statement, is it possible to do this :

 


Why is there a need to place restrictions on what elements of SAS can be used? Is this a homework problem?

Contributor
Posts: 62

Re: DataM

Posted in reply to PaigeMiller

I could use a proc sql but I think that It will use Lots of resources with large data. But I welcome anything. Thanks

 

Regards

Super User
Super User
Posts: 8,152

Re: DataM

[ Edited ]

Your "data have" statement is wrong, it doesn't read the complete data.  Is there some sort of logic behind the request, as taking a bunch of variables and outputting a gourp to each line doesn't make any sense without some sort of identifiers.  Providing a good example of the data will likely solve this.  

 

Edit, I mean from this random logic, this would work:

data want (keep=col:);
  set have (rename=(a1=col1 a4=col2 a7=col3 a10=col4 a12=col5))
        have (rename=(a2=col1...))
        have (rename=(a3=col1...));
run;

Note you might need a keep on each of those datasets, haven't tried the code, just showing that setting the data three times with renames would work.

Solution
‎08-19-2017 02:03 PM
PROC Star
Posts: 7,531

Re: DataM

Here is another way using arrays:

data have;
  input a1-a21;
  cards;
.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21
;

data want (drop=_:);
  set have (rename=(a1-a21=_a1-_a21));
  array all(*) _a1-_a21;
  array part(7) a1-a7;
  do _i=1 to 3;
    _k=0;
    do _j=_i to dim(all) by 3;
      _k+1;
      part(_k)=all(_j);
    end;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

PROC Star
Posts: 343

Re: DataM

data have;

input a1-a21;

cards;

.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21

;

 

 

data want;

set have;

array ar(*) a:;

do _n_=1 to dim(ar);

b=ar(_n_);

index+1;

output;

if mod(index,3)=0 then index=0;

end;

drop a:;

run;

 

proc sort data= want out=want1;

by index;

run;

 

proc transpose data=want1 out=want2(drop=index b _NAME_) ;

by index;

var b;

run;

 

Valued Guide
Posts: 2,187

Re: Changing data dimensions

Is that a pattern - every 4th cell belongs to the same obs? And these are all numeric?
I like the idea of reducing the process - into a single pass (well I/O is the biggest performance blocker)
Try proc iml
Respected Advisor
Posts: 4,185

Re: Changing data dimensions

@DoumbiaS

Here another coding variant using arrays.

data have(drop=_:);
  array _vars {7,3} 8;
  infile datalines dlm=' ' truncover;
  input _vars[*];
  array a {7} 8;

  do _d2=1 to dim(_vars,2);
    do _d1=1 to dim(_vars,1);
      a[_d1]=_vars[_d1,_d2];
    end;
    output;
  end;

  datalines;
.101 .201 .3 .4 .5 .6 .7 .8 .9 .10 .11 .12 .13 .14 .15 .16 .17 .18 .19 .20 .21
;
run;
Contributor
Posts: 62

Re: Changing data dimensions

So sorry for my long absence.

 

@RW9 

 

Of course it was a copy - paste error about a10 instead of a21.

Such I use large dataset with great large number of variables, optimize is efficient.

Thanks to all for responses.

 

Regards

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 262 views
  • 6 likes
  • 7 in conversation