DATA Step, Macro, Functions and more

How would I Reverse Order of All Variables, Rows, and Columns

Reply
New Contributor
Posts: 2

How would I Reverse Order of All Variables, Rows, and Columns

Hi All, 

 

I'm interested in completing a programming exercise that I've found online. I am an amateur SAS programmer looking to hone my skills through challenging coursework, and when I came across this prompt I was intrigued. However, I am stuck and unable to think of the perfect solution. 

 

The task: given any input dataset, reverse the order of all rows, columns, and values within cells. I don't know where to begin to approach this interesting question, but it has a bunch of gears turning in my head and I'm interested in decoding the logistics behind it!

 

Any input or suggestions are much appreciated! 

 

 

Super User
Posts: 6,939

Re: How would I Reverse Order of All Variables, Rows, and Columns

Posted in reply to austinrousevelt

Suggestion:  start with reversing the order of the rows.  The other pieces of the problem are much more difficult.

Contributor
Posts: 40

Re: How would I Reverse Order of All Variables, Rows, and Columns

Posted in reply to austinrousevelt

Hi,

 

Reversing of all the observations in a data set is an easy process, We can do it in many forms.one of the solution.

 

data want;
do i=nobs to 1 by -1;
set have point=i nobs=nobs;
output;
end;
stop;
run;

Super Contributor
Posts: 359

Re: How would I Reverse Order of All Variables, Rows, and Columns

[ Edited ]
Posted in reply to austinrousevelt

Hello,

 

Here is a start, using @srinath3111 solution for reversing row order.

 

proc sql noprint;
    SELECT nvar
    INTO :ncols
    FROM dictionary.tables
    WHERE libname="SASHELP" and memname="CLASS";
quit;

data _NULL_;
    set sashelp.vcolumn (where=(libname="SASHELP" and memname="CLASS")) end=eof;

    array lengths (&ncols.) $5. _TEMPORARY_;
    array columns (&ncols.) $32. _TEMPORARY_;

    lengths(&ncols.-varnum+1)=ifc(strip(type)='char',cats('$',length,'.'),cats(length,'.'));
    columns(&ncols.-varnum+1)=name;

    if eof then do;
        call execute('data want; format ');
        do i=1 to &ncols.;
            call execute(cat(' ',columns(i),lengths(i)));
        end;
        call execute('; do i=nobs to 1 by -1; set sashelp.class nobs=nobs point=i; output; end; stop; run;');

        /* reverse values of numeric variables */
        call execute('data want; set want; array nums(*) _NUMERIC_;');
        do i=1 to &ncols.;
            if substr(lengths(i),1,1) ne '$' then do;
                call execute(cat(columns(i),'=input(reverse(put(',columns(i),',',lengths(i),')),',lengths(i),');'));
            end;
        end;
        call execute('run;');
    end;

run;
Super User
Posts: 10,860

Re: How would I Reverse Order of All Variables, Rows, and Columns

Posted in reply to austinrousevelt

If "values within cells" are all a string, then that would be easy for IML code and data step.

Ask a Question
Discussion stats
  • 4 replies
  • 448 views
  • 0 likes
  • 5 in conversation