BookmarkSubscribeRSS Feed
jlin4
Fluorite | Level 6

Hi, supposed I have a list of IDs, presented in the form of a table, like work.id. For each ID, I wish to perform a set of operations that will return me either a "Y" or a "N". After which, I would like to add the returns to the table as a new column, such as in work.id_response.

 

May I enquire how should I go about doing so? Thank you!

 

data work.id;
input id$;
cards;
abc
def
ghi
jkl
;
run;
data work.id_response;
input id$ response$;
cards;
abc y
def y
ghi n
jkl n
;
run;
6 REPLIES 6
PaigeMiller
Diamond | Level 26

Something like this:

 

data want;
    set id;
    if condition=1 /* You need to type the real condition being tested*/ then response='Y'; 
    else response='N';
run;

Please note that the default behavior of a data step is to loop over all observations, so you don't need to program any loop.

--
Paige Miller
Reeza
Super User

Depends on the "set of operations" needed in that step. If it's something as simple as checking if it's in a list of value a data step works fine. If you need to go look up something in three different table and factor in amounts over time that's a lot more complex.

Your use case is too simplistic to provide an answer 😞

 

This is an example of your simplistic case - SAS loops automatically through each row in a data set so no explicit loop or anything else is required.

data want;
set id;
if id in ('abc', 'def') then response = "y";
else response = "n";
run;

 

 


@jlin4 wrote:

Hi, supposed I have a list of IDs, presented in the form of a table, like work.id. For each ID, I wish to perform a set of operations that will return me either a "Y" or a "N". After which, I would like to add the returns to the table as a new column, such as in work.id_response.

 

May I enquire how should I go about doing so? Thank you!

 

data work.id;
input id$;
cards;
abc
def
ghi
jkl
;
run;
data work.id_response;
input id$ response$;
cards;
abc y
def y
ghi n
jkl n
;
run;

 

jlin4
Fluorite | Level 6
Thank you!
ballardw
Super User

What are the rules for adding "y" or "n"?

 

The data step "loops through rows" of a data set.

 

This adds a row number (actual loop count) to your data from the work.id dataset.

data work.id_row;
   set work.id;
   row = _n_;
run;

_n_ is an automatic variable that SAS creates as it counts how many times the data step executes, i.e. "loops".

jlin4
Fluorite | Level 6
thank you!
Reeza
Super User
If one of the responses here is the correct answer to your question please mark it as resolved.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 952 views
  • 0 likes
  • 4 in conversation