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

  Hi All

        I want to calculate amounts for a large number of codes eg:

        if code in ('BBB','PPP') then amount = 1;

                but I want it to exclude the codes PPPBB and PPPCC as that code will be calculated separately eg:

        if code in ('PPPBB','PPPCC') then amount = 2;

Sorry can't get my head around this.

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

Hi,

Looks like you have everything you need. Try:

data want;

  input code $;

  if code in ('BBB','PPP') then

    amount = 1;

  else

    if code in ('PPPBB','PPPCC') then

      amount = 2;

  datalines;

PPPBB

PPP

PPPCC

BBB

;

Regards,

Amir.

Message was edited by: Amir Malik - formatting

View solution in original post

5 REPLIES 5
Amir
PROC Star

Hi,

Looks like you have everything you need. Try:

data want;

  input code $;

  if code in ('BBB','PPP') then

    amount = 1;

  else

    if code in ('PPPBB','PPPCC') then

      amount = 2;

  datalines;

PPPBB

PPP

PPPCC

BBB

;

Regards,

Amir.

Message was edited by: Amir Malik - formatting

Havi
Obsidian | Level 7

Hi Amir

The 1st in statement will select all codes that have PPP in them including PPPBB which I do not want.

Amir
PROC Star

Hi,

Looking at the data set produced, I see the following:

Obs    code     amount

1     PPPBB       2

2     PPP         1

3     PPPCC       2

4     BBB         1

Did you want something different? If yes, then please give sample input data and the output data you want to see.

Regards,

Amir.

Havi
Obsidian | Level 7

Thanks Amir 🙂

Amir
PROC Star

Hi,

No problem. As you might have realised, the in operator, as used in the code, will check if the variable is equal to any one of the values in the parentheses, not contained in one of the values.

Hope that's clear.

Regards,

Amir.

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
  • 899 views
  • 0 likes
  • 2 in conversation