Help using Base SAS procedures

Adding observations

Reply
N/A
Posts: 0

Adding observations

Can we add new observations to a dataset, not by using merge option or set statements and Proc Append?
Valued Guide
Posts: 2,108

Re: Adding observations

You can do it interactively in PROC FSEDIT or in Enterprise Guide.
SAS Super FREQ
Posts: 8,687

Re: Adding observations

...And with PROC SQL INSERT INTO.
cynthia
N/A
Posts: 0

Re: Adding observations

Hi

Thanks for reply. Actually my dataset is like below
col1 col2 col3
1 x1 a1
2 x1 a2
3 x2 b1
4 x2 b2
5 x3 c1
6 x3 c2

and i want to add labels like below with out creating new datasets for labels and not by using merge stmt or Proc Append....


col col2 col3
'A Group'
1 x1 a1
2 x1 a2
'B Group'
3 x2 b1
4 x2 b2
'C Group'
5 x3 c1
6 x3 c2

Thank You.
SAS Super FREQ
Posts: 8,687

Re: Adding observations

Hi:
This looks like a report writing task, and not something you'd store permanently in the data set. Just one reason why would wouldn't insert a variable like that into your dataset is that it looks like you want the variable "row" to "span" all 3 columns, like something you'd do in Excel. But, SAS expects every data set "row" to have the same number of columns -- so you could only insert a "spanning" row like this into a report -- not into the data set itself.

If your data were stored in a file called PERM.MYDATA, then a PROC REPORT program like this would work on a temporary data set that created variable called 'Grp' to print the identifying row above each group:
[pre]
data makegrp;
set perm.mydata;
Grp = catx(' ',upcase(substr(col3,1,1)),'Group');
run;

ods listing;
ods html file='c:\temp\makegrp.html' style=sasweb;
proc report data=makegrp nowd;
column Grp col1 col2 col3;
define Grp / order noprint;
define col1 / display;
define col2 / display;
define col3 / display;
compute before Grp;
line Grp $10.;
endcomp;
run;
ods html close;
[/pre]

Once your SAS session is over, the WORK.MAKEGRP file will go away and your original dataset is unchanged.

cynthia
N/A
Posts: 0

Re: Adding observations

Hi Cynthia,
In this case the labels are Uniform (A Group,B Group,,C Group,) ,if the labels are of different strings,for example
CATEGORY COUNT
Gender
male 12
female 10

Race
asian 12
black or african 10
american 20

Ethnicity
Hispanic or Latino 11
not Hispanic or Latino 10

in the above example CATEGORY and COUNT are variables and Gender,Race and Ethnicity are labels,these labels are not present in dataset and they are completely arbitary.Then how to insert these labels in the report.


Thank you.
SAS Super FREQ
Posts: 8,687

Re: Adding observations

Hi:
The report you describe looks like a demographic report. I'll come back to that in a minute.

The code that I posted above uses a variable, the GRP variable in order to establish a break point in the program where I can write out text. "Label" to me, is what controls the column header -- the words "Category" or "Count" in your example. What I illustrated above was writing out a custom text line at a break point using a LINE statement.

For the kind of report you describe, I'd be very tempted to use PROC FREQ, which could give you this in LISTING destination:
[pre]
Proc Freq Method

Gender Frequency
-------------------
Female 9
Male 10


Ethnic Frequency
-----------------------------------
Hispanic or Latino 12
not Hispanic or Latino 7

[/pre]

or PROC TABULATE, which could give you this in LISTING destination:
[pre]
Proc Tabulate Method

--------------------------------
|Category |Count |
|-----------------------+------|
|Total Population | 19|
|-----------------------+------|
|Gender | |
|-----------------------| |
|Female | 9|
|-----------------------+------|
|Male | 10|
|-----------------------+------|
|Ethnicity | |
|-----------------------| |
|Hispanic or Latino | 12|
|-----------------------+------|
|not Hispanic or Latino | 7|
--------------------------------

[/pre]

Or use PROC REPORT. The difference between PROC FREQ and PROC TABULATE and PROC REPORT methods is that for PROC REPORT you need to summarize the data with another procedure (I used TABULATE to create a summarized data set) and -then- use this second data set with PROC REPORT to create this output:
[pre]
Proc Report on data set from proc tabulate
Use NOPRINT on columns to hide them

Category Count
Total Population
N 19

Gender
Female 9
Male 10

Ethnicity
Hispanic or Latino 12
not Hispanic or Latino 7

[/pre]

I used SASHELP.CLASS and made up a GENDER and an ETHNIC variable. The code I ran is below.

cynthia

[pre]
data testdata;
length Gender $6 Ethnic $25;
set sashelp.class;
if sex = 'M' then gender = 'Male';
else if sex = 'F' then gender = 'Female';
if age gt 12 then ethnic = 'Hispanic or Latino';
else ethnic = 'not Hispanic or Latino';
run;

options nocenter nodate nonumber formchar='|----|+|--- ';
ods listing;
ods noptitle;
ods html file='c:\temp\category.html' style=sasweb;

proc freq data=testdata;
title 'Proc Freq Method';
tables Gender Ethnic / nocum nopercent;
label gender = ' '
ethnic = ' ';
run;

ods output table=work.tabout(rename=(n=count));
proc tabulate data=testdata f=6.;
title 'Proc Tabulate Method';
class gender ethnic;
table all gender ethnic,n /
box = 'Category' rts=25;
keylabel N='Count'
All = 'Total Population';
label gender = 'Gender'
ethnic = 'Ethnicity';
run;

proc print data=work.tabout;
title 'Summary data set from proc tabulate';
run;

** to see what all the columns look like, remove the NOPRINT;
** option from _type_, gender and ethnic;
proc report data=work.tabout nowd missing
style(lines)={just=left};
title 'Proc Report on data set from proc tabulate';
title2 'Use NOPRINT on columns to hide them';
column _type_ gender ethnic category count;
define _type_ / order order=data 'Type' noprint;
define gender / order noprint;
define ethnic / order noprint;
define category / computed 'Category';
define count / display 'Count';
compute category / character length = 25;
if ethnic = ' ' and gender = ' ' then
category = 'N';
else if ethnic = ' ' then category = gender;
else if gender = ' ' then category = ethnic;
endcomp;
compute before _type_;
if _type_ = '00' then
brkline = 'Total Population';
else if _type_ = '10' then
brkline = 'Gender';
else if _type_ = '01' then
brkline = 'Ethnicity';
line brkline $25.;
endcomp;
compute after _type_;
line ' ';
endcomp;
run;

ods html close;
[/pre]
N/A
Posts: 0

Re: Adding observations

Hi,

Thanks for reply.


Bye.
N/A
Posts: 0

Re: Adding observations

You could use dataview\edit mode\insert row, but impractical for datasets with more than 5-6 variables.
Ask a Question
Discussion stats
  • 8 replies
  • 136 views
  • 0 likes
  • 3 in conversation