BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
GN0001
Barite | Level 11

Hello team,

I have a code like this:

If member in ("A", "B) and code = "T" and payer in ("V") then thisfield = "CCC"

I want to use output statement to bucket it right after the statement is read. I can say:

How can I use output statement? How can I use thisfield along with output statement?

 

Respectfully,

CloudsInSky

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

One more tip I'll add in addition to the good advice provided so far. You can use DATA step to create multiple output data sets in one step, and use conditional OUTPUT statements to control what goes into each. This example creates one data set (WANT1) with your "CCC" records, and a second data set (WANT2) with all other records.

 

data want1 want2;
set have;
If member in ("A", "B) and code = "T" and payer in ("V") then do;
   thisfield = "CCC";
   output want1;
end;
else output want2;
run;

 

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

View solution in original post

15 REPLIES 15
PaigeMiller
Diamond | Level 26

I don't really understand the question. Bucketing the data and the OUTPUT statement are relatively independent.

 

I want to use output statement to bucket it right after the statement is read.

 

So what is stopping you?

 

 

--
Paige Miller
GN0001
Barite | Level 11

Hello team member,

 

This is what I want and I got it. 

data test1;
set sashelp.class;
format item $8.;
run;

data test2;
set test1;
if Name = "Alfred" then output = "AA";
If Name = "Alice" then output = "BB";
item = Output;
Run;


This gave me what I needed.
Thanks,
blueblue
Blue Blue
Quentin
Super User

You can use the OUTPUT statement anywhere in a DATA step.  So could could do:

 

data want;
  set have;
  If member in ("A", "B) and code = "T" and payer in ("V") then thisfield = "CCC" ;
  output;
run;

But that step would give you the same output data without the OUTPUT statement, because the DATA step has an implied OUTPUT statement. 

 

Perhaps you could show a bit more of what you are trying to do.  You could post code to create a dataset with a few records of example data, and then show the dataset you would like to create from that example data, and the code you have tried.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
WarrenKuhfeld
Ammonite | Level 13
If member in ("A", "B) and code = "T" and payer in ("V") then do;
thisfield = "CCC";
output;
end;

If you are asking to conditionally output only after setting thisfield, you would do it as above. 

ChrisHemedinger
Community Manager

One more tip I'll add in addition to the good advice provided so far. You can use DATA step to create multiple output data sets in one step, and use conditional OUTPUT statements to control what goes into each. This example creates one data set (WANT1) with your "CCC" records, and a second data set (WANT2) with all other records.

 

data want1 want2;
set have;
If member in ("A", "B) and code = "T" and payer in ("V") then do;
   thisfield = "CCC";
   output want1;
end;
else output want2;
run;

 

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
GN0001
Barite | Level 11

Hello team,

 

I want this output (want1) to be value for a variable such as productName.

 

productName =want1
prouctName = want2
an so on.

 I appreciate your help.

 

Thanks,

Blue Blue

Blue Blue
GN0001
Barite | Level 11

Hello Chris,

data want1 want2;
set have;
If member in ("A", "B) and code = "T" and payer in ("V") then do;
   thisfield = "CCC";
   output want1;
end;
else output want2;
run;

I have more than two if statements. I have 8 if statements. How can I place them in "if and else statements. Instead of data want1, want2, I have want1, want2, want3, want4, want5, want6, want7, want8.

Regards,

blue & blue

Blue Blue
GN0001
Barite | Level 11
Hello,
Thank you so very much for the response. What would be the last statement ? Else if or else?
Respectfully
Blue blue
Blue Blue
Quentin
Super User

@GN0001 wrote:
Hello,
Thank you so very much for the response. What would be the last statement ? Else if or else?
Respectfully
Blue blue

It's up to you to decide, as the programmer.  Personally, if I write IF/ELSE IF blocks, I like to explicitly handle all the expected scenarios with ELSE IF statements.  Then I add an ELSE statement which generates an error message if the data have any surprises.  e.g.:

if var='A' then do ;
  *...;
end;
else if var='B' then do ;
  *...;
end;
else if var='C' then do ;  /*else if statements for every expected value*/
  *...;
end;
else do;
  put "ER" "ROR: unexpected value " var= ;
end;

that allows your code to detect any records that fall through gaps in the logic.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
GN0001
Barite | Level 11
I am outpouring observations only not a dataset.
Thanks,
Blu blue
Blue Blue
GN0001
Barite | Level 11

Hello team;

If member in ("A", "B) and code = "T" and payer in ("V") then do;
thisfield = "CCC";
output;
end;

This syntax gives errors:

 

ERROR 117-185: There were 8 unclosed DO blocks.

Thank you for your help.

Blue Blue

Blue Blue

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 2045 views
  • 8 likes
  • 7 in conversation