在SAS中导入许多.txt文件

时间:2018-08-24 01:04:43

标签: database import macros sas pipe

我构建了将多个数据同时导入SAS的代码,但是我想改进它,有人有什么建议吗?

filename indata pipe 'dir E:\Desafio_SAS\Dados /B'; 

data  file_list; 
    length arquivos$20.; 
    infile indata truncover ;
    input  arquivos $20.;  
    call symput('num_files',_n_); 
arquivos=compress(arquivos,',.txt');
run; 

CRIANDO UMA MACRO POR PROC SQL PARA GUARDAR O NOME DOS ARQUIVOS

proc sql;
  select arquivos into :lista separated by ' ' from file_list;
quit;
%let &lista;
%macro importar(arquivo=);
filename data "E:\Desafio_SAS\Dados\&arquivo..txt";
    data &arquivo;
    infile data dlm=" " missover dsd firstobs=2;
    input v0 (v1 - v8) ($);
    format v0 F16.;
    run;
%mend importar;
%macro fileout;
%do i=1 %to &num_files;
    %importar(arquivo=df&i);
        data df&i;
        set var_names df&i;
        run;
%end;
%mend fileout;
%fileout;
%macro excluiv0;
%do i=1 %to &num_files;
    data _null_;
    data df&i(drop = v0);
    set df&i;
    run;
%end;
run;
%mend excluiv0;
%excluiv0;

这只是代码的一部分。

4 个答案:

答案 0 :(得分:2)

您可以在文件内文件规范中使用通配符。只要满足通配符的所有文件都具有相同的布局,就可以使用单个输入读取所有文件。

示例

* create three text files having same fields;

data _null_;
  file '%temp%\1.txt';
  put '1 2 3 abc';
  put '3 4 5 def';

  file '%temp%\2.txt';
  put '6 7 8 ghi';
  put '9 10 11 jkl';

  file '%temp%\3.txt';
  put '12 13 14 xyz';
  put '15 16 17 tuv';
run;

* read all three using wildcard in infile.  Save name of file whence 
* data cometh frometh;

data want;
  length _filename_ $250;
  infile '%temp%\?.txt' filename=_filename_;
  length source $250;
  length a b c 8 s $20;

  source = _filename_;
  input a b c s;
run;

通配符是

  • ?,任何字符的0或1个
  • *,任意数量的任意字符

答案 1 :(得分:0)

t1 <- ttheme_default(core=list(
  fg_params=list(fontface=c("bold.italic")),
  bg_params = list(fill=c("green", "grey90","blue","red"))))

grid.arrange(g1,
             tableGrob(iris[1:5, 1:4], theme = t1,, rows=NULL), 
             g1, g1, nrow = 2)

答案 2 :(得分:0)

---
title: "Column Orientation"
output: flexdashboard::flex_dashboard
---

```{r setup, include=FALSE}
library(ggplot2);library(knitr);library(kableExtra)
library(flexdashboard);library(gridExtra);library(grid)
```   

<style>
  .colored {
  background-color: #002080;}
</style>

Column{data-width=200}
-------------------------------------

### Chart 1{.colored}

```{r}
gauge(10, min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
gauge(50, min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
gauge(20, min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
gauge(15, min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
gauge(5 , min = 0, max = 100, sectors = gaugeSectors(colors = "#002080"))
```

Column
-------------------------------------

### Chart 2

```{r, include=FALSE}
tt1 <- ttheme_default()
tt2 <- ttheme_minimal()
tt3 <- ttheme_minimal(
  core=list(bg_params = list(fill = blues9[1:4], col=NA),
            fg_params=list(fontface=3)),
  colhead=list(fg_params=list(col="navyblue", fontface=4L)),
  rowhead=list(fg_params=list(col="orange", fontface=3L)))

tab <- grid.arrange(tableGrob(iris[c(1:4,1:2), c(1:3,1:2)], theme=tt3), nrow=1)
graf <- ggplot(data=mtcars, aes(x=drat, y=disp, group=vs)) +
            geom_line() + ylab("") +
            geom_point()

gg.gauge <- function(pos,breaks=c(0,10,25,100)) {
  get.poly <- function(a,b,r1=0.5,r2=1.0) {
    th.start <- pi*(1-a/100)
    th.end   <- pi*(1-b/100)
    th       <- seq(th.start,th.end,length=1000)
    x        <- c(r1*cos(th),rev(r2*cos(th)))
    y        <- c(r1*sin(th),rev(r2*sin(th)))
    return(data.frame(x,y))
  }
  ggplot()+ 
    geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y),fill="forestgreen", colour = "white", size = 1.2, alpha = 0.7) +
    geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y),fill="gold", colour = "white", size = 1.2, alpha = 0.7) +
    geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y),fill="red", colour = "white", size = 1.2, alpha = 0.7) +
    geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y), colour = "white")+
    annotate("text",x=0,y=0,label=pos,vjust=0,size=8,fontface="bold")+
    coord_fixed()+
    theme_bw()+
    theme(axis.text=element_blank(),
          axis.title=element_blank(),
          axis.ticks=element_blank(),
          panel.grid=element_blank(),
          panel.border=element_blank()) 
}
gg1 <- gg.gauge(2,breaks=c(0,10,25,100))
gg2 <- gg.gauge(5,breaks=c(0,10,25,100))
gg3 <- gg.gauge(7,breaks=c(0,10,25,100))

```

```{r, fig.width=9.5, fig.height=7}
for (i in 1:5){

  title1=textGrob("Test title TESTE", gp=gpar(fontface="bold", fontsize = 15))
  lay <- rbind(c(3,3,4,4,5,5),
               c(1,1,1,1,1,1),
               c(1,1,1,1,1,1),
               c(2,2,2,2,2,2),
               c(2,2,2,2,2,2))
  grid.arrange(graf, tab, gg1, gg2, gg3, top=title1,
               layout_matrix= lay)
  grid.rect(width = 1, height = 1, gp = gpar(lwd = 2, col = "black", fill = NA))

  cat("\n")
}
```

答案 3 :(得分:-1)

---
title: "BRADESCO"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
---


```{r setup, include=FALSE}
library(ggplot2);library(knitr);library(kableExtra)
library(flexdashboard);
library(gridExtra);library(grid)
```

Geral {data-icon="fa-signal"}
===================================== 

### Chat 1

```{r}
p1 <- qplot(mpg, wt, data = mtcars, colour = cyl)
p2 <- qplot(mpg, data = mtcars)
p3 <- qplot(mpg, data = mtcars, geom = "dotplot")
  lay <- rbind(c(1,1,1,2,2,2),
               c(3,3,3,3,3,3))
grid.arrange(p2, p3, p1, nrow = 2, layout_matrix= lay)
```   

### Table 1

```{r}
kable(mtcars[1:10, c(1:6,1:4)], caption = "Group Rows") %>%
  kable_styling("striped", full_width = F) %>%
  group_rows("Group 1", 4, 7) %>%
  group_rows("Group 2", 8, 10)
```   

Por segmento {data-icon="fa-signal"}
===================================== 

<style>
  .colored {
  background-color: #002080;}
</style>

Row{data-height=200}
-------------------------------------

### Chart 1{.colored}

```{r, fig.width=55}
dat = data.frame(count=rep(c(10, 60, 30),10), category=rep(c("A", "B", "C"),10), fator=c(1,2,3,4,5))

# Add addition columns, needed for drawing with geom_rect.
dat$fraction = dat$count / sum(dat$count)
dat = dat[order(dat$fraction), ]
dat$ymax = cumsum(dat$fraction)
dat$ymin = c(0, head(dat$ymax, n=-1))


p <- ggplot(dat, aes(x=2, y=fraction, fill=category))+
  geom_bar(stat="identity", colour = "white", size = 2) +
  xlim(0, 2.5) +
  scale_fill_manual(values=c("#002080", "#002080", "white")) +
  coord_polar(theta = "y")+
  labs(x=NULL, y=NULL)+ guides(fill=FALSE) +
  ylab("fsfagafs") + facet_wrap(~ fator,nrow = 1) +
  annotate("text", x = 0, y = 0, label = "WW", size = 20, colour = "white") +
  theme(
        plot.margin = margin(-1.1, 3.6, -1.1, 3.6, "cm"),
        panel.spacing = unit(30, "lines"),
        axis.ticks=element_blank(),
        axis.text=element_blank(),
        axis.title=element_blank(),
        panel.grid=element_blank(),
        plot.background = element_rect(fill = "#002080", colour="#002080"), 
        panel.background = element_rect(fill = "#002080", colour="#002080"),
        strip.background = element_blank(),
        strip.text.x = element_blank())

p
```


Row
-------------------------------------

### Chart 2 {data-wight=900}

```{r, include=FALSE}
tt1 <- ttheme_default()
tt2 <- ttheme_minimal()
tt3 <- ttheme_minimal(
  core=list(bg_params = list(fill = blues9[1:4], col=NA),
            fg_params=list(fontface=3)),
  colhead=list(fg_params=list(col="navyblue", fontface=4L)),
  rowhead=list(fg_params=list(col="orange", fontface=3L)))

tab <- grid.arrange(tableGrob(iris[c(1:4,1:2), c(1:3,1:2)], theme=tt3), nrow=1)
graf <- ggplot(data=mtcars, aes(x=drat, y=disp, group=vs)) +
            geom_line() + ylab("") +
            geom_point()

gg.gauge <- function(pos,breaks=c(0,10,25,100)) {
  get.poly <- function(a,b,r1=0.5,r2=1.0) {
    th.start <- pi*(1-a/100)
    th.end   <- pi*(1-b/100)
    th       <- seq(th.start,th.end,length=1000)
    x        <- c(r1*cos(th),rev(r2*cos(th)))
    y        <- c(r1*sin(th),rev(r2*sin(th)))
    return(data.frame(x,y))
  }
  ggplot()+ 
    geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y),fill="forestgreen", colour = "white", size = 1.2, alpha = 0.7) +
    geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y),fill="gold", colour = "white", size = 1.2, alpha = 0.7) +
    geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y),fill="red", colour = "white", size = 1.2, alpha = 0.7) +
    geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y), colour = "white")+
    annotate("text",x=0,y=0,label=pos,vjust=0,size=8,fontface="bold")+
    coord_fixed()+
    theme_bw()+
    theme(axis.text=element_blank(),
          axis.title=element_blank(),
          axis.ticks=element_blank(),
          panel.grid=element_blank(),
          panel.border=element_blank()) 
}
gg1 <- gg.gauge(2,breaks=c(0,10,25,100))
gg2 <- gg.gauge(5,breaks=c(0,10,25,100))
gg3 <- gg.gauge(7,breaks=c(0,10,25,100))

```

```{r, fig.width=7.2, fig.height=7}
for (i in 1:5){

  title1=textGrob("Test title TESTE", gp=gpar(fontface="bold", fontsize = 15))
  lay <- rbind(c(3,3,4,4,5,5),
               c(1,1,1,1,1,1),
               c(1,1,1,1,1,1),
               c(2,2,2,2,2,2),
               c(2,2,2,2,2,2))
  grid.arrange(graf, tab, gg1, gg2, gg3, top=title1,
               layout_matrix= lay)
  grid.rect(width = 1, height = 1, gp = gpar(lwd = 2, col = "black", fill = NA))

  cat("\n")
}
```


### Chart 2
```{r}
 mydata = data.frame(x1 = c(1,2,3),
                        x2 = c(9,8,7),
                        label = c("description a",
                                  "description b",
                                  "description c"))
ht = 5
wd1 = 5
wd2 = 12
gap = 0.1
nc = ncol(mydata)
nr = nrow(mydata)

x = rep(c(seq(0,(nc-2)*(wd1+gap), wd1+gap), (nc-2)*(wd1+gap) + gap + 0.5*(wd2+wd1)), nr)
y = rep(seq(0,(nr-1)*(ht+gap), ht+gap), nc) %>% sort()
h = rep(ht, nr * nc)
w = rep(c(rep(wd1, nc-1), wd2), nr)
info = as.vector(t(as.matrix(mydata[nr:1,])))

df = data.frame(x = x, y = y, h = h, w = w, info = info)

ggplot(df, aes(x, y, height = h, width = w, label = info)) +
  geom_tile() +
  geom_text(color = "white", fontface = "bold") +
  coord_fixed() + 
  scale_fill_brewer(type = "qual",palette = "Dark2") +
  theme_void() +
  guides(fill = F)
```   

teste