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.
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.
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.
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;
@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.
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.
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!
Ah, there you go then - always use otherwise!
Dive into keynotes, announcements and breakthroughs on demand.
Explore Now →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.