s.koch
s.koch

Reputation: 27

putting data into character variable sas

I'm facing the problem that I want to put data into a character variable. So I have a long tranposed dataset where I have three variables: date( by which i transposed before hand) var (has three different outputs of my previous variables) and col1 (which includes the values of my previous variables). Now i want to create a forth variable which has as well three different outputs. My problem is that I can create the variable put with my code it does always create missing value.

data pair2;
 set data1;
if var="BNNESR" or var="BNNESR_r" or var="BNNESR_t" then output;
length all $ 20;
all=" ";
if var="BNNESR" then all="pdev";
if var="BNNESR_t" then all="trigger"; 
if var="BNNESR_r" then all="rdev";
drop var;
run;

Afterwards I want to tranpose it back by the "all" variable. I know i could just rename the old vars before I transpose it and then just keep them. But the complete calculation will go on and actually will be turned into a macro where it is not that easy if would do it like that way.

Upvotes: 0

Views: 157

Answers (1)

Tom
Tom

Reputation: 51611

Your program will just subset the input data and add a new variable that is empty because you are writing the data out before you assign any value to the new variable.

Use a subsetting IF (or WHERE) statement instead of using an explicit OUTPUT statement. Once your data step has an explicit OUTPUT statement then SAS no longer automatically writes the observation at the end of the data step iteration.

data pair2;
  set data1;
  if var="BNNESR" or var="BNNESR_r" or var="BNNESR_t" ;
  length all $20;
  if var="BNNESR" then all="pdev";
  else if var="BNNESR_t" then all="trigger"; 
  else if var="BNNESR_r" then all="rdev";
  drop var;
run;

Since the list in the IF statement matches the values in the recode step then perhaps you want to just use a DELETE statement instead?

data pair2;
  set data1;
  length all $20;
  if var="BNNESR" then all="pdev";
  else if var="BNNESR_t" then all="trigger"; 
  else if var="BNNESR_r" then all="rdev";
  else delete;
  drop var;
run;

Upvotes: 1

Related Questions