BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LB3
Calcite | Level 5 LB3
Calcite | Level 5

I need some help using a lookup table in SAS that looks like the following. The table comes from an Excel file that produced by someone else and I have no way to change the structure of the table. I would like to be able use the table to join where the Term and Date in the first row match another table. Any way to accomplish this in SAS?

 

Term6/1/20206/2/20206/3/20206/4/2020
110111213
29101112
3891011
478910
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@LB3 wrote:

The table comes from an Excel file that produced by someone else and I have no way to change the structure of the table.


Why do you have no way to change the structure? Import the data from Excel and then use PROC TRANSPOSE and a standard merge after that. Assuming your date variables are named something like _date1 _date2 this may work. There are ways to easily list all your other variables if you don't have a naming convention.

 

proc transpose data=dates out=long_dates;
by term;
var _: ; *you need to put the list of variable names here;
run;

Transposing data tutorials:
Wide to Long:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/

https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

View solution in original post

3 REPLIES 3
AhmedAl_Attar
Ammonite | Level 13

Try this

 

LIBNAME exfiles XLSX '[drive:]\test_xlsx_lkup.xlsx';
options validvarname='any';

proc contents data=exfiles._all_; run;
proc sql;
	select term
		,'43986'n
	from exfiles.sheet1;
quit;

Additional examples can be found here

Using LIBNAME XLSX to read and write Excel files 

 

Accessing Excel files using LIBNAME XLSX

 

Hope this helps,

Ahmed

Reeza
Super User

@LB3 wrote:

The table comes from an Excel file that produced by someone else and I have no way to change the structure of the table.


Why do you have no way to change the structure? Import the data from Excel and then use PROC TRANSPOSE and a standard merge after that. Assuming your date variables are named something like _date1 _date2 this may work. There are ways to easily list all your other variables if you don't have a naming convention.

 

proc transpose data=dates out=long_dates;
by term;
var _: ; *you need to put the list of variable names here;
run;

Transposing data tutorials:
Wide to Long:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/

https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

RichardDeVen
Barite | Level 11

How are you reading the Excel table ?

 

If you Proc IMPORT the  Excel file the date valued columns in SAS will be named _<excel-date-number>.

Perform a transpose BY TERM and compute the SAS date value from the _NAME_ value.

 

Example (untested)

Proc IMPORT datafile='SomeoneElse.xlsx' dbms=xlsx replace out=lookup_raw;
Proc TRANSPOSE data=lookup_raw out=lookup_base;
by TERM;
var _:;
run;
data lookup;
set lookup_base;
xl_date = input (substr(_name_,2),8.);
date = xl_date + '31dec1899'd;
format date date9.;
run;

Now you can join

select
   ...
   , COL1 as LookupValue
from 
  other_table as A
join 
  lookup as LU
on 
  A.date = LU.date and A.TERM=LU.TERM

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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