Q:

sas matrix to dataset with variable names

I have following matrix:

I would like to covert it to SAS dataset in following format:

lastname is missing if it is the same as the previous one, and I have thousands of names. How should I do this?

A:

You can tell SAS to import the "matrix" without names. This will both let you be able to read the names as data and also force all variables to be character strings.  So essentially you will get something like this data set.
data have ;
  length f1-f6 $32 ;
  infile cards dsd truncover ;
  input f1-f6 ;
cards;
,,L1,,L2
,,F1,F2,F3,F4
,,,,,,                  
L3,F5,1,2,3,4
,F6,5,6,7,8
L4,F7,9,10,11,12
,F8,13,14,15,16
;;;;

You can then process the dataset and generate your from/to name variables.  First make an array that references all of the variables.  Then make two temporary arrays to store the column header names.  Then when you read each row you can find the names and values from that row and output them.
data want ;
  set have nobs=nobs ;
  array x _character_ ;
  array ln (200) $32 _temporary_;
  array fn (200) $32 _temporary_;
  length from_lastn lag_lastn from_firstn to_lastn to_firstn $32 ;
  from_lastn = coalescec(x(1),lag_lastn);
  lag_lastn = from_lastn ;
  retain lag_lastn ;
  from_firstn = x(2) ;
  if _n_=1 then do i=3 to dim(x) ;
    ln(i)=coalescec(x(i),ln(i-1));
  end;
  if _n_=2 then do i=3 to dim(x) ;
    fn(i)=x(i);
  end;
  if _n_>3 then do i=3 to dim(x) ;
    to_lastn = ln(i);
    to_firstn= fn(i);
    value=input(x(i),comma32.);
    output;
  end;
  keep from_lastn from_firstn to_lastn to_firstn value ;
run;

