BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASdevAnneMarie
Barite | Level 11

Hello Experts,

I have an empty table like with column Id , Somme

 

I would like to create the table :

Id Somme
1  

 

My code is :

data matable2;
	set matable;
	attrib n length=8.;
	TOTAL2_OC=propcase((put(total,NLMNLEUR12.0)));
	input n;
	cards;
	1
;
	
run;

But this code doesn't work. Could you help me ?

Thank you !

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

If your table is empty of rows, then output will not create a row.  There are several methods, such as appending data to the table, or insertin via sql, e.g.

/* Create empty table for example */
proc sql;
  create table empty (id num, somme num);
quit;

/* Use insert sql method */
proc sql;
  insert into empty set id=1;
quit;

/* Use append */
data inter;
  id=1;
run;
data empty;
  set empty inter;
run;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

When code doesn't work, show us the log (if there are errors or other problems in the log).

 

When code gives the wrong output, show us the wrong output and explain what is wrong.

--
Paige Miller
SASdevAnneMarie
Barite | Level 11
Thank you ! My log is :
29 data OC2;
30 set OC;
31 attrib n length=8.;
32 TOTAL2_OC=propcase((put(EA_total,NLMNLEUR12.0)));
33 input n;
34 cards;

NOTE: There were 0 observations read from the data set WORK.OC.
NOTE: The data set WORK.OC2 has 0 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


36 ;
37
38 run;
39
40
PaigeMiller
Diamond | Level 26

I'm afraid I still don't understand, you talk about "add a row in empty table" but you are not showing us an empty table, the table you showed has one row. 

 

To add a row to an existing (empty or non-empty) table, you can just use the OUTPUT command;

 

data want;
    set have end=eof;
    output;
    if eof then output;
run;

 

This adds a row to the table that is identical to the last row. That's probably not what you want, but I don't really know what you do want. You might want an empty row (that's not what you said you wanted), which isn't hard to do, but I can't think of a good reason to add an empty row to a table.

 

If the above code is not what you want, please show us clearly the starting table, and the desired output table.

--
Paige Miller
SASdevAnneMarie
Barite | Level 11
Thank you !
I have an empty table (no data in rows, but I have the columns).
I would like to add to this table the column n with data 1 (n=1).
I don't know how to do it.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

If your table is empty of rows, then output will not create a row.  There are several methods, such as appending data to the table, or insertin via sql, e.g.

/* Create empty table for example */
proc sql;
  create table empty (id num, somme num);
quit;

/* Use insert sql method */
proc sql;
  insert into empty set id=1;
quit;

/* Use append */
data inter;
  id=1;
run;
data empty;
  set empty inter;
run;
SASdevAnneMarie
Barite | Level 11
Thank you !
Tom
Super User Tom
Super User

@SASdevAnneMarie wrote:
Thank you !
I have an empty table (no data in rows, but I have the columns).
I would like to add to this table the column n with data 1 (n=1).
I don't know how to do it.

Let's try to translate this.  You have a DATASET with zero OBSERVATIONS.  And you want to add a new VARIABLE named N. 

data want;
  set have;
  length N 8 ;
run;

But for the new variable to have a value you will need to add an actual observation.

 

Remember that most SAS data steps to not end at the last statement in the data step.  Instead they end when they read past the end of the input.  So you need to control for the situation where the source dataset has zero observations, otherwise the data step will stop at the SET statement and never write out any observations.

 

If you know that the dataset does not have observations (or you don't want to copy any of the observations) then you can write the data step so that the dataset is referenced, but it is never read, so it cannot read past the end of the data.

data want;
  if 1=2 then set have;
  n=1;
run;

Since the IF condition can obsviously never be true the SET statement will never execute.  But when the step is compiled the SET statement is seen and so all of the variables defined in HAVE will become part of WANT.

 

Example:

1687  data want;
1688   if 0 then set sashelp.class;
1689   n=1;
1690  run;

NOTE: DATA STEP stopped due to looping.
NOTE: The data set WORK.WANT has 1 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


1691  proc print;
1692  run;

NOTE: There were 1 observations read from the data set WORK.WANT.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 second
Obs    Name    Sex    Age    Height    Weight    n

 1                     .        .         .      1

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 4454 views
  • 2 likes
  • 4 in conversation