BookmarkSubscribeRSS Feed
kbwork
Calcite | Level 5

I am trying to create a field 'respondent_id' that contains either the s_id, m_id or c_id depending on what activity the user preformed. The data step below works but the issue I'm having is that some users did multiple acts and it is only storing one ID per user.  I want the code to just create a new line if the user has more than one activity instead of overwriting the other.

data act.3;

set act.2;

by user_id;

retain user_id s_id m_id c_id Respondent_id;

  if user_id and act_id in (28 29) then do;

  respondent_id=c_id;

  end;

  if user_id and act_id in (27) then do;

  respondent_id=m_id;

  end;

  if user_id and act_id in (25) then do;

  respondent_id=s_id;

  end;

run;

3 REPLIES 3
Kurt_Bremser
Super User

if user_id and ...

treats user_id as a boolean variable which is considered true if not 0.

How is act_id stored? The way you handle it, there cannot be more than one activity per record, so all activities will be output.

The log of the data step might be helpful.

PGStats
Opal | Level 21

act.3 is not a valid dataset name. This datastep cannot run, contrary to what you are saying. With valid dataset names you might try :

data three;

set two;

if user_id > 0 then do;

    select (act_id);

    when (28, 29) respondent_id=c_id;

    when (27) respondent_id=m_id;

    when (25) respondent_id=s_id;

    otherwise;

    end;

end;

run;

PG

PG
Reeza
Super User

Use an OUTPUT statement to control the output.

I don't know what you're expecting the user_id statement in the IF statement to accomplish. It will evaluate to true if the user_id is greater than 0.

Perhaps something like this:

data act.three;

set act.two;

by user_id;

  if user_id and act_id in (28 29) then do;

  respondent_id=c_id;

output;

  end;

  if user_id and act_id in (27) then do;

  respondent_id=m_id;

output;

  end;

  if user_id and act_id in (25) then do;

  respondent_id=s_id;

output;

  end;

run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1385 views
  • 0 likes
  • 4 in conversation