Export different sized tables to same excel tab in SAS - excel

I have two tables A and B that have a different number of columns, with absolutely no match between the columns names but one differentiator (let's call it ID).
I'm programming a macro in SAS, so that it outputs an excel file such as:
each sheet within the excel is corresponding to an ID.
within each sheet, I have:
content of table A
empty line
content of table B
The problem is that I can't append rows of data in SAS because columns are non matching.
Any thoughts?
Thanks for your help!

You can use DDE for that - Dynamic Data Exchange Protocol. Basically what it does is simulating user's commands and clicks on various menus and buttons in Excel (and also in Word and some other applications) - or, more exactly, issues commands on now obsolete Macro Language for Excel ver. 4 (X4ML).
So, using DDE, you can in SAS program launch Excel, open or create workbook, create tabs (spreadsheets), put your data into specified cells range, format any single cell or range etc.
Here's a good intro into this topic:
http://www2.sas.com/proceedings/sugi26/p011-26.pdf

Related

Create Connection to one raw data file for multiple excel files

I currently have one excel file with four worksheets with data (Name: target value 2022.xlsx). This data is used in multiple excel files to make calculations and to show the values using VLookUp. Until now I copy-paste the values from this one file with four worksheets into all the other files with those four worksheets (and more) when one value changes throughout the year. It also seems to be problematic when a new year begins and a new "target value 2023.xlsx" is required. I tested a lot of ways to make a connection, but nothing seems to be the perfect way:
copy-paste each table via VBA (current way, but I don't want to open every file just because one value changed and click the "Refresh" button)
external reference Cell A1: =[target value 2022.xlsx]Table1!E3 (if one column is deleted, the connection shows #REF!)
Data > New Query > From File > From Workbook (if one column is deleted, the power query doesn't work anymore)
Data > From Text (only works, if all four worksheets are in four seperate csv-files, not optimal)
Data > From Access (seems to be the best way to get the data from four different tables within the database???)
What's the best way to do this, if multiple people use it? The values in "target value 2022.xlsx" change multiple times a year and many users need different files where the data is required. Thank you!

Select Data for excel Graph sees zeroes when I load the data using sql

I have a problem with Excel (2016)/ VBA macros. I am happy to provide more detail or copies of files if required.
In summary this is what is happening:
I have a workbook with five worksheets. Three contain data representing both axes of a matrix (1&2), the cells of the matrix (3), one sheet on which data from sheets 1 to 3 are brought together (4) and - finally - a graph worksheet (5 - based on a range in 4).
The reason I do it like this is that I want to graph parts of the data at a time otherwise it is all too confusing. A simple form is used to choose which column of the data-set to display and to also allow stepping through the list of items to display so just part of the range is in view at a time. Variables are set in cells on sheet 4 (outside the graphed range) when the buttons are clicked on the form. The formulae that populate the graphed range on that sheet use these variables to choose the required data from sheets 1 to 3.
Initially I populated the data in sheets 1 to 3 using macros to read from text files. These files had to be populated from a database before opening the Excel workbook. I decided to "Simplify" this process by changing the macros to query the database directly.
It took a while to get this going and all looked fine. I can see the data in the first four worksheets is populated correctly (including being able to click on the form buttons and change the contents of the graphing range) but the graph sees values of zero in every cell. When I right click on the graph and select "Select Data" and then choose an item in the "Legend Entries (Series)" list in the left-hand box, and the click "Edit", I see the cell ranges of both the Series name and the Series Values are correctly displayed, the preview of "Name" has the correct value but the preview of "Series" shows a string of comma-separated zeroes.
I can switch worksheets to view the "GraphData" on sheet 4 while still keeping the dialogue box open and see the actual cells that are being referred to and they are not 0, 0, 0... they are 78, 69, 44...
Where are the zeroes coming from?
I have even saved the workbook - keeping changes - so the contents of sheets 1 to 3 are kept, put a stop in the "Workbook_Open" macro so it doesn't run, and still I get zeroes when I re-open it.
Sorry to bother folk. I worked out the problem. As usual, there were interesting lessons along the way.
I started out with a query that populated a text file with data from my database. Because I wanted the file to be ingested by Excel as a csv file, the output for each row of the selected data was a single string column containing the keys and the data values with commas between them, the numbers being converted to VARCHAR within the sql so I could concatenate them into the string.
Them when I decided to try using the query directly from Excel rather than having the file step in between, I removed the concatenation operators and the commas and put a comma between them in the sql syntax but didn't remove the conversion to VARCHAR of the numbers.
With QueryTable, when you import a text file, one of the optional properties is an array of codes representing the data types of the incoming data. I assumed that with a direct query, Excel would assign a data type based on content as it does for typed values (unless you specify another). Clearly Excel and the source database exchange information, in addition to the data set itself, that tells Excel the data type of the columns - as sent.
The graphing function was seeing my data as strings so assigned values of zero. I began by putting a Value() around my lookup functions on the GraphData worksheet. That worked but then I went further back and actually changed the Sql so it sent numeric data without conversion to VARCHAR and then it worked without the Value() change.
It never occurred to me that Excel wouldn't just see a number and treat it as such - not that Sql Server would have told Excel it was a string.

Limit with creating a drop-down list dependent on a selection in excel

I have an excel file with two sheets. The second sheet (Report) contains data validation cells based on the first sheet (Data). From the second sheet, the drop-down list that displays in the Select XXX depends on the selection in the Generate Report. When the Generate Report is set to anything beyond the first five in its list, the "Select XXX" displays year as a default list (no problem with this) via the code ...INDIRECT("Year").... The problem is that excel does not allow for addition of more code (seems I hit the limit). The question is - how can I manipulate this code to accommodate every option in the Generate Report? or perhaps, is there another method to implement?
The data validation source code for the drop-down list is =IF($B$4=Data!$Q$5,INDIRECT("Client"), IF($B$4=Data!$Q$6,INDIRECT("Month"), IF($B$4=Data!$Q$7,INDIRECT("Product_Service"), IF($B$4=Data!$Q$8,INDIRECT("Sector"), IF($B$4=Data!$Q$9,INDIRECT("Trans_Type"),INDIRECT("Year"))))))
Please, see the sample file at https://drive.google.com/file/d/1VKkGHjlJzLQqx4J9kyd_bCKG4r0Q7HkG/view?usp=sharing
What you could do is put the range names in column R, and VLOOKUP them:
=IFERROR(INDIRECT(VLOOKUP($B$4,Data!$Q$5:$R$9,2,FALSE)),INDIRECT("Year"))
You could then have as many item lists as you wish.

Microsoft Excel. How to put all tables from all lists to a single list

Is it possible somehow to do the following in Microsoft Excel?
Every list has a single table with its own Header:
Table may be of any size, and number of such lists is unknown in advance.
I need to gather all those tables in the very first list which is called "Main" so that each table just followed by another like this:
I need it only for printing, so sorting and another stuff like this is unnecessary.
Microsoft Excel - 2019.
I think you can achieve what you are after using named ranges per this video.
The ranges are then accessible from the dropdown to the left of the formula bar.
I find it easy to select area of tables first, then define a named range based on currently selected area, rather than entering it in the prompt per the video.
1. Insert -> Name -> Define Name
If you have a workbook with multiple sheets you can define names on any sheet, So lets's say table1 was in sheet1, and table2 in sheet2. If sheet1 was the active sheet, you could still select the named range of table2, and Excel will automatically flip sheets for you and select that area as if you had just highlighted the region.
Later when it comes to printing, select the named area, and the say
2. File -> Print Area -> Set Print Area
Then do:
3. Print and instead of selecting Active Sheet use Selection
(I am talking about doing it on a Mac V16.22 - but should be similar on Windows too)
EDIT:
There is IMPORTRANGE in Google Sheets.
Not sure if something similar in Excel was what you were after.
So take a look at this too.

Extracting data to different tabs

I have a spreadsheet that I will be importing data into from multiple sources and I want to be able to extract the data to other tabs based on the descriptions. For example, I want to pull all of the rows that say "Credit Canadian Deposit Settlement" in Column B and put them into a separate tab so that I can work with them.
Since I have to do this with a large amount of data each week I would like to have as much of it as possible automated so that I can just import that data and then start working with it without too much manipulation. I would eventually like to have a tab for each different description.
spreadsheet sample
This could be done with Power Query:
https://www.excelcampus.com/powerquery/power-query-overview/
Otherwise you it would be better to write a macro. Start by recording a macro using the following steps:
Select cell A1
Insert a table
Filter for one of the descriptions
Press ctrl-A to select entire range
Copy
Paste to the target spreadsheet
Then review and update the code:
Replace any references to a fixed range with a general range, such as:
Range("A1").CurrentRegion
Then you can also insert a loop to run through each possible value. Search/ask for help on writing loops in VBA for further info.

Resources