DATA Step, Macro, Functions and more

How to determine the last cell in Excel using SAS?

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

How to determine the last cell in Excel using SAS?

Hi!

For example, I have this 4 X 4 table in SAS (it has 4 columns, and 4 rows excluding the headers).

If exported to excel, the range would be translated to A1Smiley Very Happy5.

I need help how to get the D5 value.

Thank you!


Accepted Solutions
Solution
‎12-04-2014 04:15 AM
Super Contributor
Posts: 340

Re: How to determine the last cell in Excel using SAS?

Posted in reply to angeliquec

If this is about the text "D5", then you could calculate it in SAS using the dataset you would like to export:

Data A;
  Input X1-X4;
  Datalines;
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
;
Run;

Data xx;
  Length Text_Col $2.;
  MyFile=Open("A");
  Rows=Attrn(MyFile,"NObs")+1; * including Header;
  Cols=Attrn(MyFile,"NVars");
  If (Cols le 26) Then Text_Col=Byte(64+Cols);
  Else If (Cols ge 27) & (Cols le 676) Then Text_Col=(Byte(64+Int(Cols/26)))!!Byte(64+Mod(Cols,26));
  ExcelCell=Compress(Text_Col!!Put(Rows,Best8.));
  Put "**" ExcelCell "**"; * "D5" is in the log ... ;
  MyFile=Close("A");
Run;

View solution in original post


All Replies
Super User
Posts: 19,822

Re: How to determine the last cell in Excel using SAS?

Posted in reply to angeliquec

You could manually calculate it.

Why though? Excel also supports RC notation which may be easier to derive. RC=row/column so A1Smiley Very Happy5 would be R1C1:R5C4

Solution
‎12-04-2014 04:15 AM
Super Contributor
Posts: 340

Re: How to determine the last cell in Excel using SAS?

Posted in reply to angeliquec

If this is about the text "D5", then you could calculate it in SAS using the dataset you would like to export:

Data A;
  Input X1-X4;
  Datalines;
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
;
Run;

Data xx;
  Length Text_Col $2.;
  MyFile=Open("A");
  Rows=Attrn(MyFile,"NObs")+1; * including Header;
  Cols=Attrn(MyFile,"NVars");
  If (Cols le 26) Then Text_Col=Byte(64+Cols);
  Else If (Cols ge 27) & (Cols le 676) Then Text_Col=(Byte(64+Int(Cols/26)))!!Byte(64+Mod(Cols,26));
  ExcelCell=Compress(Text_Col!!Put(Rows,Best8.));
  Put "**" ExcelCell "**"; * "D5" is in the log ... ;
  MyFile=Close("A");
Run;

Super User
Super User
Posts: 7,970

Re: How to determine the last cell in Excel using SAS?

Posted in reply to angeliquec

Hi,

What is the purpose, are you trying to write to a specific range?  I fso then you may be better of with libname to Excel.

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 260 views
  • 0 likes
  • 4 in conversation