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;

 

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!

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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
WarrenKuhfeld
Rhodochrosite | Level 12
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;

 

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!
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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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