Help using Base SAS procedures

Errors in accounting for null values

Frequent Contributor
Posts: 131

Errors in accounting for null values

Here is the code

data test;

set test1;

if date ge anydate;


proc format $cfmt  '075'='fs075'





proc transpose data= test out = test2  format step_cd $cfmt.

id step_cd;

by ln_no;

var anydate;


proc sql;

create table test4 as

select a.ln_no,




from mytable  a

left join test2  b

on a.ln_no=b.ln_no;


This works when the fs varaibles have data.  When they are blank or null, I get errors.  The vairable is not found or in correlation with b.  How can I account for the variables in my code if they happen not to have values on a particular day?  I this case the highlighted variables of fs075 079 or 080??

Super User
Posts: 10,483

Re: Errors in accounting for null values

if the value doesn't exist then it can't be transposed to create the variable.

One crude approach would be to create a data set with no values but the variables you need.

Then after the transpose:

data test2;

     set emptyset test2;


then the sql

Super User
Posts: 17,784

Re: Errors in accounting for null values

Your proc transpose code doesn't look correct, missing a semi colon and the format looks to be in the wrong place.

Depending on your data you may be best off adding it in prior to transposing using the preloadfmt option with proc tabulate/summary/means or after using some other method.

I could probably use some imagination and guess what your input table looks like, but its easier if you post sample data and expected output. A data step can also be used to efficiently transpose data.

Super User
Super User
Posts: 6,499

Re: Errors in accounting for null values

Simplest way is to just use another data step to add the variables you want. Has the advantage of also allowing you to set the order, format, labels etc.

Note also that you can just use the PREFIX option on PROC TRANSPOSE to do what the FORMAT is doing for you.

data test;

  input ln_no id step_cd $ anydate date9. ;

  format anydate date9. ;


1 1 075 01JAN2013

1 2 079 01FEB2013

2 3 079 02FEB2013

2 4 075 01MAR2013


proc print data=test; run;

%let possible_vars = fs075 fs079 fs080 ;

proc transpose data=test out=test1 prefix=FS ;

  by ln_no;

  id step_cd;

  var anydate;


proc print data=test1; run;

data test2;

  retain ln_no ;

length &possible_vars 8;

  format &possible_vars date9. ;

  set test1 (drop=_name_);


proc print data=test2; run;

Obs ln_no id step_cd  anydate

1   1      1     075  01JAN2013

2   1      2     079  01FEB2013

3   2      3     079  02FEB2013

4   2      4     075  01MAR2013

Obs ln_no _NAME_  FS075     FS079

1   1     anydate 01JAN2013 01FEB2013

2   2     anydate 01MAR2013 02FEB2013

Obs ln_no fs075        fs079        fs080

1   1     01JAN2013    01FEB2013        .

2   2     01MAR2013    02FEB2013        .

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation