BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
d6k5d3
Pyrite | Level 9

From a dataset I need to filter one kind of value, and create a separate dataset  with that value. But before doing that I need to create a column. So what I am trying to do is:

 

data x1;

set zzz;

if x=123 then a=1 and output x1; ===> I know this doesn't work.

run;

 

So, my new dataset x1 will have a new column 'a' with 1 for each x=123 in each row. How can I do both in one datastep?

 

Much thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

Oh - that's still quite straight-forward.

data code1 code2 code3 code4 code5;
set master;
select(event);
   when('xxxxx') do;
         code = 'code1';
         output code1;
         end;
   when('yyyyy') do;
          code = 'code2';
          output code2;
          end;
   when('zzzzz') do;
          code = 'code3';
          output code3;
          end;
   when('aaaaa') do;
          code = 'code4';
          output code4;
          end;
   when('bbbbb') do;
          code = 'code5';
          output code5;
          end;
   otherwise;
   end;

Since they're all distinct or mutually exclusive conditions, a select statement is the way to go. And you can write them all out in one pass of your source dataset.

View solution in original post

11 REPLIES 11
LaurieF
Barite | Level 11

It's much simpler than you thought. There's two ways:

data z1;
set zzz;
if x = 123;
a = 1;
output;
run;

But because you're only creating one dataset, the output statement is implied:

data z1;
set zzz;
if x = 123;
a = 1;
run;

Note the if statement. It's known as a subsetting if - only when it is true are the statements after it executed.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

this will give you all of your records and includes a variable named a that is set to the value of 1 for only records where x = 123

data x1;
set zzz;
if x = 123 then a = 1;
run;

 this will output only if x = 123

data x1;
set zzz;
if x = 123 then do;
a = 1;
output;
end;
run;

 

d6k5d3
Pyrite | Level 9

@LaurieF@VDD, I beg your pardon because I think my message has not been clear. This is what I would do, and I also looking for ways to do it using macro.

 

I have a master file. In that I have a column called 'event'. There are 10 events out of which I need 5 events. For each event I need to give a code, and generate an output having the name of the code for each event. I am aiming to do this in one datastep. So, with repetition I would code it the following way. 

 

data code1;

set master;

if event="xxxxx" then code="code1";

else delete;

run;

 

data code2;

set master;

if event="yyyyy" then code="code2";

else delete;

run;

... … … (I will repeat the above code 3 more times with changes where needed)

 

what would be an efficient way to accomplish the above task? can I do this using macro? I think that would be fantastic.

 

Regards.

LaurieF
Barite | Level 11

Oh - that's still quite straight-forward.

data code1 code2 code3 code4 code5;
set master;
select(event);
   when('xxxxx') do;
         code = 'code1';
         output code1;
         end;
   when('yyyyy') do;
          code = 'code2';
          output code2;
          end;
   when('zzzzz') do;
          code = 'code3';
          output code3;
          end;
   when('aaaaa') do;
          code = 'code4';
          output code4;
          end;
   when('bbbbb') do;
          code = 'code5';
          output code5;
          end;
   otherwise;
   end;

Since they're all distinct or mutually exclusive conditions, a select statement is the way to go. And you can write them all out in one pass of your source dataset.

d6k5d3
Pyrite | Level 9
Thank you so much. I am gonna try this. Need a clarification. What is the function of 'otherwise' here? And, don't we need 'run;' at the end?
LaurieF
Barite | Level 11

Technically, the otherwise isn't needed. I always include it, because it saves a potential warning/error message.

 

And yes, put a run to make it work!

d6k5d3
Pyrite | Level 9
SAS was giving me an error without the 'otherwise'!

ERROR: Unsatisfied WHEN clause and no OTHERWISE clause at line 6245 column 1
LaurieF
Barite | Level 11

Ah, there you go then - always use otherwise!

d6k5d3
Pyrite | Level 9
Encountered another problem! Is it possible to write multiple 'when' 'conditions'? Like:

data code1 code2 code3 code4 code5;
set master;
select(event);
when('xxxxx') or when ('xyxyxy') do;
code = 'code1';
output code1;
end;
… … … …
LaurieF
Barite | Level 11

Much simpler:

when('xxxxx', 'xyxyxy') do;

This is what you need: Select statement

d6k5d3
Pyrite | Level 9
I am grateful to you for all your time and help! I wish I had been a little more careful when I was reading the 'when statement' earlier.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1148 views
  • 7 likes
  • 3 in conversation