Subset Numeric variable and Character variable.

Reply
Occasional Contributor
Posts: 16

Subset Numeric variable and Character variable.

I have one data set contain char segment1 (first segment, second segment and third segment) and num form_quality(good, bad, average). What command I should use to subset first segment and good?

Contributor
Posts: 37

Re: Subset Numeric variable and Character variable.

Hi

Could you please provide some sample Input data set and the output you want ? your query is pretty high level

Thanks

Occasional Contributor
Posts: 16

Re: Subset Numeric variable and Character variable.

Posted in reply to Karthikeyan

person id    form_quality              segment1

   6                 Good form               first Segment

    4                 no info                    first Segment

   7                    no info                  third Segment

  9                  Average form          third Segment

   15                     Bad form             third Segment

  8                     no info                    third Segment

    11                   no info                    third Segment

I am not is this the one you requested. This somehow the data, I want only the observation with Bad form and third Segment, but the problem is form_quality is numeric data whereas segment1 is char variable.

Frequent Contributor
Posts: 144

Re: Subset Numeric variable and Character variable.

Hi,

If I have understood properly, then a useful way to subset a dataset is with an IF condition or a WHERE.

Data segments_good;

  set Have;

  WHERE segment1="first segment" and form_quality>=7;

run;

If you provide a minimum example with an example with your data, we could give a better answer

Occasional Contributor
Posts: 16

Re: Subset Numeric variable and Character variable.

Posted in reply to arodriguez

Thanks for answer! Why the form_quality>=7?

Frequent Contributor
Posts: 144

Re: Subset Numeric variable and Character variable.

I say 7 because form is supose to be numeric, mi fist thoug was a numeric variable from 0-10 as exemple and depending of the value the quality is better or worse.

If your var is categoric as in your example then you could use something like the answered by  MadhuKorni, or specifie the levels of the numeric format applied to form_quality

Contributor
Posts: 65

Re: Subset Numeric variable and Character variable.

You mentioned form quality as Num and gave the values as good,bad, average

If its is character type then you can use any of the below code.

proc sql;

create table Want as

select * from Have

where upcase(segment1) = "FIRST SEGMENT" and upcase(form_quality) = "GOOD";

quit;

data Want;

set Have;

if upcase(segment1) = "FIRST SEGMENT" and upcase(form_quality) = "GOOD";

run;

If you are sure about the case of the data then you can remove upcase() for both the variables.

If form_quality is Numeric then mention the number in the condition by removing the quotes.

If you have any format ( numeric to char conversion) on form_quality variable (1- good,2-average,3-bad) then use

data Want;

set Have;

if upcase(segment1) = "FIRST SEGMENT" and form_quality = 1;

run;

Ask a Question
Discussion stats
  • 6 replies
  • 382 views
  • 6 likes
  • 4 in conversation