BookmarkSubscribeRSS Feed
bayzid
Obsidian | Level 7

Is there a easier way to transpose a dataset from log to wide using proc sql that does not require sorting the dataset?

Can the transposed data automatically create numeric variables if all the values are such?
The example code is applied on the following dataset where some of the variables, such as "Isvariable" will only have numbers but they are appearing as character variables?
proc transpose data=tall out=wide(drop=_name_);
by filename row ;
id name;
var value;
run;

bayzid_0-1681970576877.png

 

2 REPLIES 2
Kurt_Bremser
Super User

Since value is of type character, all transposed variables will also be character.

Use a DATA step with SELECT:

data want;
set have;
by row;
retain
  /* put your new variables here */
;
if first.row then call missing (/*all newvariables*/);
select (name);
  ......
  when ('IsCytotoxic') iscytotoxic = input(value,1.);
  ......
end;
keep row /* all new variables */;
run;
s_lassen
Meteorite | Level 14

It looks like your data is read from a number of infiles, row by row.

 

In that case, it may be (just guessing) that your data is grouped (but not sorted, because of the filename) already. Then it may be a possibility to use NOTSORTED:

proc transpose data=tall out=wide(drop=_name_);
by filename row notsorted;
id name;
var value;
run;

But your output variables will still be character, as the input variable is character.

 

If you need to convert the data to numeric, you can code the stuff in a datastep, as suggested by @Kurt_Bremser , although I would probably code it like this:

data wide;
  do until(last.row);
    set tall;
    by filename row notsorted;
    select(upcase(name));
       ...
       when('ICYTOTOXIC') Icytotoxic=input(value,1.);
       ...
       otherwise /* maybe write a warning in the log here */;
       end;
    end;
  drop name;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 470 views
  • 0 likes
  • 3 in conversation