Joe Englishh
Joe Englishh

Reputation: 13

SAS. Create a field if not exist

I'm very noob in SAS.

I want to check if a value exist based on a date.

Example. I have a table with dates 1may to 3may and a variable called y. I want to check if y="hi" in every day. If it doesn´t exist, I want to create the field.

input

| date | y |
|:-----|:--|
|1may  |la |
|1may  |le |
|1may  |hi |
|2may  |la |
|2may  |le |
|3may  |la |
|3may  |le |
|3may  |hi |

output

| date | y |
|:-----|:--|
|1may  | la|
|1may  | le|
|1may  |hi |
|2may  |la |
|2may  |le |
|2may  |hi |
|3may  |la |
|3may  |le |
|3may  |hi |

Sorry for my english. Thank you

Upvotes: 0

Views: 512

Answers (2)

Tom
Tom

Reputation: 51581

Looks like you want to add an observation, not make any new variable (aka FIELD).

One way to do this is to make a dataset with one observation per DATE and with 'hi' as the value of Y and then combine that with the existing dataset.

So if you input dataset is named HAVE and it is already sorted by DATE and Y your code could be.

data all_dates;
   set have ;
   by date ;
   if first.date ;
   y= 'hi';
   keep date y;
end;

data want ;
  merge all_dates have ;
  by date y ;
run;

Upvotes: 1

Richard
Richard

Reputation: 27508

You can assign the result of a logical evaluation to a variable. An evaluation will return 0 for false and 1 for true.

Example:

data want;
  set have;
  hi_flag = (y='hi');  * new variable hi_flag contains result of test (y='hi');
run;

Upvotes: 0

Related Questions