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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

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;

fayyazcivil
Calcite | Level 5

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??

Tom
Super User Tom
Super User

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;

fayyazcivil
Calcite | Level 5

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;

Tom
Super User Tom
Super User

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

fayyazcivil
Calcite | Level 5

Perfect! . Thank you very much

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2620 views
  • 6 likes
  • 2 in conversation