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 !
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;
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.
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.
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 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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.