BookmarkSubscribeRSS Feed
CMANOJ622
Calcite | Level 5

Hi All,

I am having a SAS dataset and I need extract to only diagonal data point from the data set and print  the same one line.  Please help me with SAS code if any.

 

For ex:  

FROMTOJun-24Jul-24Aug-24Sep-24Oct-24Nov-24Dec-24Jan-25Feb-25Mar-25Apr-25May-25Jun-25
BALANCE_(01)BALANCE__(02)002325262522252825202821
BALANCE_(02)BALANCE__(03)00026232720232826222627
BALANCE__(03)BALANCE__(04)0000242222242423202322
BALANCE__(04)BALANCE__(05)000002326222025202320
BALANCE__(05)BALANCE__(06)00000026252027232121
BALANCE__(06)BALANCE__(07)0000000262627252121
BALANCE__(07)BALANCE__(07)000000002025272026
BALANCE__(07)BALANCE__(08)00000000025222623
BALANCE__(08)BALANCE__(09)0000000000282822
BALANCE__(09)BALANCE__(10)000000000002826
BALANCE__(10)BALANCE__(11)00000000000024
               
               
Result Jun-24Jul-24Aug-24Sep-24Oct-24Nov-24Dec-24Jan-25Feb-25Mar-25Apr-25May-25Jun-25
Balance 002326242326262025282824
2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

I suppose you got the data from Excel. Storing data in variable names is usually a bad idea.

This does what you want:

 

data HAVE;
  input JUN_24 JUL_24 AUG_24 SEP_24 OCT_24 NOV_24;
cards;
0 0 1 11 21 31 
0 0 0 2 22 32   
0 0 0 0 3 23   
0 0 0 0 0 4
run;

data WANT;
  array IN[6] JUN_24 JUL_24 AUG_24 SEP_24 OCT_24 NOV_24;  
  array OUT[6];  
  retain OUT: 0;
  set HAVE end=LASTOBS;
  do I=1 to dim(IN);
    if IN[I] then do;
      OUT[I]=IN[I];
      leave;
    end;
  end;
  if LASTOBS then output;
  keep OUT:;
  rename OUT1=JUN_24 OUT2=JUL_24 OUT3=AUG_24 OUT4=SEP_24 OUT5=OCT_24 OUT6=NOV_24;
run;  
JUN_24 JUL_24 AUG_24 SEP_24 OCT_24 NOV_24
0 0 1 2 3 4

 

 

 

Ksharp
Super User

It is SAS/IML things.

 

options validvarname=v7;
proc import datafile='C:\Users\xiakeshan\Downloads\Test_1.xlsx' out=have dbms=xlsx replace;run;

proc iml;
use have;
read all var _num_ into num[c=vname1];
close;
vname=putn(inputn(compress(vname1,'_'),'best.')+'01jan1900'd-2,'monyy.');
n=j(1,ncol(num),.);
do i=1 to ncol(num);
 if isempty(loc(num[,i]^=0)) then m=1;
  else m=max(loc(num[,i]^=0));
 n[i]=num[m,i];
end;
create want from  n[c=vname];
append from n;
close;
quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 317 views
  • 4 likes
  • 3 in conversation