BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Pauliet123
Calcite | Level 5
Hello,
I want to only keep observations with a specific value that could be found in multiple columns. Example:
Person 1st 2nd 3rd
A Pizza. Chicken. Fruit
B. Chicken. Pizza. Fruit
C. Fruit. Chicken. Fish

I want to only keep observations where pizza is a listed option regardless of what column it is in. There are 10+ columns so I didn’t want to do an if then for all those columns. Thank you so much!
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15
data have;
input Person $1. (_1st _2nd _3rd) (:$10.); /* _ added because variable can't atar with a number*/
cards;
A Pizza. Chicken. Fruit.
B Chicken. Pizza. Fruit.
C Fruit. Chicken. Fish.
;
run;
proc print;
run;

data want;
  set have;
  array food[*] _1st--_3rd;
  if "Pizza." in food;
run;
proc print;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Onyx | Level 15
data have;
input Person $1. (_1st _2nd _3rd) (:$10.); /* _ added because variable can't atar with a number*/
cards;
A Pizza. Chicken. Fruit.
B Chicken. Pizza. Fruit.
C Fruit. Chicken. Fish.
;
run;
proc print;
run;

data want;
  set have;
  array food[*] _1st--_3rd;
  if "Pizza." in food;
run;
proc print;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

Having a period after the value in columns 1 and 2, but not in column 3, seems to complicate this.

 

@Pauliet123  Is that really representative of the actual data? Or is that something you just put in there while typing your original question and doesn't represent the actual data?

 

If the actual data has periods in columns 1 and 2 but not in column 3, then the solution from @yabwon fails if "Pizza" (with no period) appears in column 3.


As always, good data layouts with repeatable formatting of the data makes programming easier. Unusual data layouts and/or inconsistent formatting of the data make programming harder.

--
Paige Miller

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 2 replies
  • 675 views
  • 0 likes
  • 3 in conversation