Hello.
I have two tables. 1st order is 61312*2 (two columns i.e. origin and destination, values ranges from 1 to 81 in both columns).
2nd table is 81*81 (include length between origin and destination)
Now I need to create a table having order 61312*3 i.e origin*destination*length, i.e. to write a code which take the value from 2nd matrix (length) for their respective values in 1st matrix.
Kindly let me know how should I proceed.
You can either add the aliases that I left out to the FROM clause
from have A left join lookup B
or just use the table names as the aliases.
select HAVE.*,LOOKUP.length
Assume your first table is named HAVE and your second in named MATRIX. You need to convert the second so that it is has the ORIGIN and DESTINATION as actual variables instead of row# or col#. Then just can just merge the two tables.
data lookup ;
set matrix;
array val _numeric_;
origin = _n_;
do destination = 1 to 81 ;
length = val(destination);
output;
end;
run;
proc sql ;
create table want as select a.*,b.length
from have left join lookup
on a.origin = b.origin
and a.destination = b.destination
;
quit;
Dear Tom, thank you very much for this.
As you said the second table name Matrix, how can I define the Input variable in this case, i.e. origin1-origin81??
Not sure I understand the question. How to input the data depends on the form that the data is currently in. If you have it in a CSV file with 81 lines of 81 numbers then this program will read it into the a table with three columns and 81x81 rows.
data lookup ;
infile 'matrix.csv' dsd truncover length=2000 ;
origin+1;
do destination=1 to 81 ;
input length @;
output;
end;
run;
Dear Tom,
Yes, the above coding has worked well. Now the problem is when I run it with proc sql, the following error occurs:
415 proc sql ;
416 create table want as select a.*,b.length
417 from have left join lookup
418 on a.origin = b.origin
419 and a.destination = b.destination
420 ;
ERROR: Unresolved reference to table/correlation name a.
ERROR: Unresolved reference to table/correlation name b.
ERROR: Unresolved reference to table/correlation name a.
ERROR: Unresolved reference to table/correlation name b.
ERROR: Could not expand a.*, correlation name not found.
ERROR: Unresolved reference to table/correlation name b.
ERROR: The following columns were not found in the contributing tables: a.
421 quit;
You can either add the aliases that I left out to the FROM clause
from have A left join lookup B
or just use the table names as the aliases.
select HAVE.*,LOOKUP.length
Perfect! . Thank you very much
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.