BookmarkSubscribeRSS Feed
Anandkvn
Lapis Lazuli | Level 10
data insertvalues;
set sashelp.class;
run;


data newvalues;
set insertvalues  ;
 output; 
if mod(_n_,2)=0 then do  ;
 name='Anand';
 sex='M';
 age=16;
 Height= 8.11 ;	Weight= 54.22;
 output;
 end;
 proc print;
 run;

how to insert values conditionally in proc sql

9 REPLIES 9
Kurt_Bremser
Super User

Maxim 14. Use the Right Tool.

Here, it is the data step, which you already use. Forget SQL whenever sequence matters, and using it here would be pure idiocy.

PGStats
Opal | Level 21

That something can't or shouldn't be done is precious knowledge. And being told so is not convincing for some. I suspect that such is the purpose of this exercise.

PG
Kurt_Bremser
Super User

@PGStats wrote:

That something can't or shouldn't be done is precious knowledge. And being told so is not convincing for some. I suspect that such is the purpose of this exercise.


True, true.

ballardw
Super User

@PGStats wrote:

That something can't or shouldn't be done is precious knowledge. And being told so is not convincing for some. I suspect that such is the purpose of this exercise.


I also wonder how well the poster is translating or understanding the original assignment.

 

Juola's metatheroem: You will seldom misunderstand a problem so as to make it simpler.

Dr. Robert Juola was my first professor in statistics and made this comment after several assignments ...

 

 

Kurt_Bremser
Super User

If this is another of your useless homeworks (what imbecile comes up with such stupid exercises for things you will NEVER do in real life, and where the time could have been used for teaching meaningful things like hash objects, instead of being a total waste?), here some hints:

  • Use OUTER UNION CORR to stack the dataset with itself
  • as the first source, use the variables as they are, and create a new one (_name) from name, and another (_flag) as 0
  • as the second source, use name as _name, create all others (name,sex,age,height,weight) with your values, and _flag as 1
  • order by _name and _flag
  • drop both new variables with a dataset option from the result

As you will see, the code will be unwieldy and ugly as hell, and just an illustration what being a total idiot entices. Scratch this experience from your memory ASAP, it only wastes precious neurons.

Ksharp
Super User
You can't "insert values conditionally in proc sql".
INSERT statement in SQL is like PROC APPEND, can't like data step update obs conditionally.
Oligolas
Barite | Level 11

Hi,

there is a way...

PROC SQL ;
   CREATE VIEW CLASS AS
      SELECT monotonic() AS _N_,*
      FROM sashelp.class
   ;
   CREATE VIEW class_hlp AS
      SELECT monotonic()+.1 AS _N_,
             'Anand' AS name,
             'M' AS sex,
             16 AS age,
             8.11 AS Height,
             54.22 AS Weight
      FROM CLASS
   ;
   CREATE TABLE want(DROP=_N_) AS
      SELECT *
      FROM CLASS
      UNION CORRESPONDING ALL
      SELECT *
      FROM class_hlp
      WHERE (mod(int(_N_), 2)=0)
      ORDER BY _N_
   ;
   DROP VIEW class, class_hlp;
QUIT;
________________________

- Cheers -

Ksharp
Super User
You create a NEW dataset ,not insert into original dataset .
Oligolas
Barite | Level 11

That's exactly what I do 🤗 and meet the requirement 🎉

I conditionally insert values in a dataset with PROC SQL

________________________

- Cheers -

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