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

Hi Community,


Well this my code 

 

DATA ABC;

INFILE DATALINES MISSOVER DSD;

INPUT N1 N2 MS1$ MS2$;

DATALINES;

10,12,MS,

10,12,MS

10,12, ,MS

10,12,MS,

;

RUN;

 

PROC SQL;

   CREATE TABLE WORK.BBC

   SELECT N1, 

          N2, 

          ms1, 

          ms2, 

          

            (CASE WHEN(N1<N2)THEN MS1 OR MS2 END) AS FINAL_MS

      FROM WORK.ABC;

QUIT;

 

IN BBC  i want to create a new variable i.e. FINAL_MS in order to receive values from either ms1 or ms2 but it returning me logical value that is 1 as n1<n2.  My need is it must receive either value from ms1 or ms2. How to do this ?  thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Attyslogin
Obsidian | Level 7

 

 

PROC SQL;

CREATE TABLE CBC AS

 

SELECT N1,N2,MS1,MS2,CASE WHEN(N1<N2) THEN MS1 OR MS2 END AS FINAL_MS FROM ABC;

QUIT;

 

------------------------------------------------- following code worked well---------------------------------------

 

PROC SQL;

CREATE TABLE CBC AS

 

SELECT N1,N2,MS1,MS2,CASE WHEN(N1<N2) THEN COALESCE(MS1,MS2) END AS FINAL_MS FROM ABC;

QUIT;

 

thanks ballardw

 

View solution in original post

6 REPLIES 6
ballardw
Super User

Please provide details on which value, MS1 or MS2, should be returned. Your current code or description does not provide anything to tell use which of those values should be returned.

Attyslogin
Obsidian | Level 7

As i said i want to populate final_ms by getting values either MS1 variable or MS2 variable.

And values of MS1 & MS2 are already listed in datalines.  

PGStats
Opal | Level 21

CASE WHEN N1 < N2 THEN MS1 ELSE MS2 END

PG
Attyslogin
Obsidian | Level 7

PG i have other conditions too to write further, cant simply put ms2 in else. 

simply all i want is final_ms should take value either from ms1 or ms2 whenever condition becomes true (n1<n2). thats it.

ballardw
Super User

Lets try this again:

 

Your data currently shows that you may have missing for one of MS1 or MS2 variables. If that is always the case and you mean that you want the non-missing value as the result then perhaps you want

 

CASE WHEN(N1<N2)THEN coalescec(MS1,MS2) END

 

It really helps to show the exact desired result from your example data.

Attyslogin
Obsidian | Level 7

 

 

PROC SQL;

CREATE TABLE CBC AS

 

SELECT N1,N2,MS1,MS2,CASE WHEN(N1<N2) THEN MS1 OR MS2 END AS FINAL_MS FROM ABC;

QUIT;

 

------------------------------------------------- following code worked well---------------------------------------

 

PROC SQL;

CREATE TABLE CBC AS

 

SELECT N1,N2,MS1,MS2,CASE WHEN(N1<N2) THEN COALESCE(MS1,MS2) END AS FINAL_MS FROM ABC;

QUIT;

 

thanks ballardw

 

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