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

Data aa;
input X$ Z $;
datalines;
eliy N
eliya Y
ais N
shsi N
;
run;

 

I want to add a column ‘K’. If column'Z' contains Y, then all values in column 'K' are Y, else are N.

Who could tell me ....if I could do this in just one data step or one proc sql?I don't hope to first get a dataset then merge or join.

 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

Not tested :
Proc sql;
CREATE TABLE want AS
SELECT a.*, b.K
FROM aa AS a
LEFT JOIN (
SELECT CASE WHEN SUM(Z="Y") THEN "Y" ELSE "N"
END AS K
FROM aa
) AS b
ON 1;
quit;

View solution in original post

5 REPLIES 5
gamotte
Rhodochrosite | Level 12

Hello,

Not tested :
Proc sql;
CREATE TABLE want AS
SELECT a.*, b.K
FROM aa AS a
LEFT JOIN (
SELECT CASE WHEN SUM(Z="Y") THEN "Y" ELSE "N"
END AS K
FROM aa
) AS b
ON 1;
quit;

cecily
Calcite | Level 5

oh!!This is what I want! Thank you very much!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Or to simplfy:

proc sql;
  create table WANT as
  select  *,
          case when exists(select distinct Z from AA where Z="Y") then "Y"
               else "N" end as K
  from    AA;
quit;

 

Actually, saying that:

 

proc sql;
  create table WANT as
  select  *,
          coalesce((select distinct Z from AA where Z="Y"),"N") as K
  from    AA;
quit;
PBsas
Obsidian | Level 7

Are you looking for something like this?

 

data want;

set aa;

if z = "Y" then K = "Y";

else k = "N";

run;

cecily
Calcite | Level 5

No. what I want is all values for that new column are ‘Y’ if column 'Z' contains 'Y'.  I have got the right answer. Thank you very much!

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
  • 1151 views
  • 1 like
  • 4 in conversation