BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
deega
Quartz | Level 8

Hello Everybody,

 

I want to create a new variable (Count) based on multiple queries in a group:

Observations are grouped by ID. Condition 1 is : Desired variable 'count' increases by 1 if variable 'Country' is US. Condition2 is if variable 'Group' is 'ABC' and variable 'Name' contains 'ian' then also count increases by 1. Something like below :

InputOutput
IDDateNameGroupCountryCount
11月2日JianABCAU1
11月3日BianDEFRU1
11月4日LasieABCUK1
11月5日BratJKLUS2

 

I wrote the following program but dont know why its not giving the desired result.

 

data Out;
set Input ;
by ID Date ;
if first.ID then count=0;
if (find(Name,' ian゙','i') and Group = 'ABC') then count + 1 ;
else if Country="US" then count + 1;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

In the code you provide, there is a space before ian (and a special character after).

View solution in original post

5 REPLIES 5
gamotte
Rhodochrosite | Level 12

Hello,

In the code you provide, there is a space before ian (and a special character after).

deega
Quartz | Level 8
Thanks...
Any idea how to filter the records that do not have 'ian' and group abc ??
gamotte
Rhodochrosite | Level 12

You can explicitely output only the records that satisfy the condition with an "output" statement.

Whenever a data step contains an output statement, the automatic output of input records

is disabled.

 

data want;
set Input;
by ID Date;
if first.ID then count=0;
if (find(Name,'ian','i') and Group = 'ABC') then do;
	count + 1 ;
	output;
end;
else if Country="US" then count + 1;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Always a good idea to post test data, in the form of a datastep.  To get those with the string reverse the logic gate and include any extras, from:

if index(name,'ian') > 0 then ...

To

if index(name,'ian') = 0 and group ne "abc" then ...

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The text string ' ian゙' is not found in any of the rows of Name.  If you want to find Jian and Bian then:

if index(name,'ian') > 0 then ...

 

Also, good idea to post test data in the form of a datastep.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 5 replies
  • 908 views
  • 1 like
  • 3 in conversation