1.7.10

Dynamic concatenation of transposed variables

Prepared by Jose Abraham

The transpose procedure in SAS changes multiple values in rows (for a column) into columns, and can also change multiple columns’ values into multiple rows values for a single column. The number of variables (columns) to be created in the output file is based on the maximum number of observations (rows) in a variable (column) to be transposed. If the prefix= option is not specified, then the transposed variables have names Col1, Col2 etc…. i.e. if the dataset has 4 observations in a variable, then on transposing 4 variables namely Col1, Col2, Col3 and Col4 are obtained against each value of the by variable (labparam, lab_id in the below example).

In certain situations, it is required to concatenate the columns obtained during transposing. This can be attained simply by setting the dataset and concatenating the variables using the concatenation operator (||).

e.g. Newvar= Col1|| ‘,’||Col2|| ‘,’||Col3|| ‘,’||Col4;

This procedure may however not produce desirable results if any of the four columns have missing values. If suppose Col3 and Col4 have missing values then consecutive ‘,’ will be presented as the last few characters in the variable Newvar.

The concatenation is thus done by determining the maximum number of Col variables in the transposed dataset and then concatenating the non missing variables to create the new variable against each of the by variables.

Consider the following example from a clinical trial analysis, where the laboratory values are collected at different laboratories. For different laboratories, the reference ranges adopted for parameters are different.Such a dataset is shown below


While obtaining descriptive statistics for different lab parameters, the lab ranges are to be presented in the report as a sub header of the following form.

e.g. Param1

Lab-A: 0-35 g/L (Age 0-99)

Lab-B: 0-34 g/L (Age 0-18), 0-35 g/L (Age 18-60), 0-36 g/L (Age 60-99)

For different laboratory groups the number of ranges will be different, so it is necessary to dynamically concatenate the ranges for each lab for each parameter. The steps involved in this process are given below

  1. Dataset with the lab ranges is transposed.

    *Transposing the data to get the reference ranges horizontally;

    proc transpose data=range out=range_trn;

    by labparam lab_id;

    var range;

    run;

  2. Select the information about the number of columns in the transposed dataset with the help of sashelp.vcolumn dataset. A macro variable named ‘coln’ is created with the maximum number of ‘Col’ variables as its value.

    *Creating a macro variable with maximum number of columns;

    data _null_;

    set sashelp.vcolumn end=last;

    where trim(libname) = 'WORK' and trim(memname) ='RANGE_TRN' and upcase(substr(name,1,3))='COL';

    if last then call symput('coln',trim(left(put(_n_,6.))));

    run;

  3. Set the transposed dataset and giving array reference to the ‘Col’ variables. Concatenate the ranges with using a do loop and then remove the preceding comma by taking substring of the concatenated variable. A variable named ‘labrange’ is created by concatenating with the lab_id.

    data range_con;

    length convar $400;

    array cols {&coln.} $50 col1-col&coln.;

    set range_trn;

    do i=1 to dim(cols);

    if not missing(cols[i]) then convar= trim(convar)||', '||cols[i];

    end;

    convar=substr(left(convar),2);

    Labrange=trim(left(lab_id))||':'|| compbl(convar);

    keep labparam lab_id labrange;

    run;

The dataset range_con contains the concatenated ‘labrange’ variable.

*Transposing the dataset to present the ranges for different lab_id's in different variables;

proc transpose data= range_con out=lab_rng(drop=_name_);

id lab_id;

by labparam;

var labrange;

run;

The output dataset obtained during transposing is of the following form

By merging it with the descriptive statistics dataset and using ‘labparam’ and the variables containing the concatenated lab ranges (here LAB_A and LAB_B) as by variables in proc report creates a report which contains descriptive statistics for one parameter per page. By using the ‘#byval’ option in the title statement we can obtain the sub headers of the following type.

Param1

Lab-A: 0-35 g/L (Age 0-99)

Lab-B: 0-34 g/L (Age 0-18), 0-35 g/L (Age 18-60), 0-36 g/L (Age 60-99)





Project specific SAS shortcuts

Prepared by Jose Abraham

 

As a SAS programmer, an important concept to know when opening and saving SAS programs is the ‘SasInitialFolder’. The location of this folder is the path name where SAS reads or stores files when a specific drive and pathname is not given. The name of this path appears in a small panel located in the lower right-hand corner of our SAS session. This can be changed at any time with a double-click within that rectangle to access a dialogue box, then entering the drive and path to the desired folder in the space provided or by moving through the folders as done while searching for a particular file. But the change which is made is not permanent. During the next invocation of the SAS System, the current folder will by default present the current folder path that was permanently set. In SAS v8 for windows, the default folder may be something like “C:\Documents and Settings\<user name>\My Documents\My SAS Files\V8” or a path which is in the drive where SAS is installed (e.g. “E:\SAS Installation”). When trying to open or save a program in a SAS session, this default folder will be shown and changing this folder to the desired folder may consume some amount of our valuable time. Here is the way to save this time and also to make project specific SAS shortcuts, which will allow opening and saving SAS programs of that project from the ‘SAS Programs’ folder which is set for that project. The steps to set the current folder to a new location are given below. The folder which is to be set should be created before going through the following steps. 1. Create a shortcut of SAS by clicking on the ‘Start’ menu and select ‘SAS’ from ‘All Programs’, right click on it and then select ‘Send to Desktop’. Then a new SAS shortcut icon will be created in the desktop.2. Right click on the icon and select ‘Rename’ and give a name related to the project.3. Again right click on the icon and select ‘Properties’ and click on the ‘Shortcuts’ tab.4. In the ‘Target’ box, go to the far end of the right-hand side of what is seen in that by pressing the ‘End’ button. Type one space, and enter the following command exactly as you see it here: -sasInitialFolder="."5. The current or working SAS folder will be the pathname that is specified in the 'Start in' field (located just below the 'Target' field). To designate the choice of the current folder, enter the drive and pathname of it within that box. This folder needs to exist on the specified directory.e.g. “G:\Project1\SAS programs”6. Click on ‘Apply’ and then click ‘OK’. The shortcut is now ready for use for the particular project and it will direct to the specified folder on clicking ‘File-> Open’ or ‘File->Save As”.