180 likes | 280 Views
Trucs et astuces. Souvent, on me demande encore. Retenir certaines colonnes. Étape data : set Fichier (keep= CITY STATE STATECODE STATENAME); set Ficher (keep=CITY STAT:);. Retenir certaines colonnes , option 2. macro Garder : %macro garder(Table,Cherche); %global varlist;
E N D
Trucs et astuces Souvent, on me demande encore...
Retenir certaines colonnes • Étape data : set Fichier (keep= CITY STATE STATECODE STATENAME); set Ficher (keep=CITY STAT:);
Retenircertainescolonnes, option 2 • macro Garder : %macro garder(Table,Cherche); %global varlist; %let dsid = %sysfunc(open(&Table,i)); %let varlist =; %do i=1 %to %sysfunc(attrn(&dsid,nvars)); %if %index(%sysfunc(varname(&dsid,&i)),%sysfunc(upcase(&Cherche))) gt 0 %then %let varlist=&varlist %sysfunc(varname(&dsid,&i)); %end; %put varlist=&varlist; %mend garder;
Importer un fichier plat • SASHELP.ZipCode: proc contents data=SASHELP.ZipCode out=Attributs(keep=NAME VARNUM TYPE LENGTH); run; proc sort data=Attributs; by VARNUM; run; data Attributs; set Attributs; if TYPE = 1 then Format = compress(LENGTH||'.'); else Format = compress('$'||LENGTH||'.'); run;
Importer un fichier plat (suite) data _null_; length Ligne $5000.; file Put_File; put 'data Source;'; Ligne = 'infile "/home/gen00279/LaCapitale/BackUps/Source.txt" firstobs=2 '||"delimiter=';' MISSOVER DSD lrecl=5000;"; put @4 Ligne; dsid = open('Attributs','i'); Ligne = ''; do Roule = 1 to attrn(dsid,'nobs'); rc = fetch(dsid); Variable = getvarc(dsid,varnum(dsid,'NAME')); Format = getvarc(dsid,varnum(dsid,'Format')); Ligne = trim(Ligne)||' '||compress(Variable)||' '||compress(Format); end; rc = close(dsid); Ligne = 'length'||trim(Ligne)||';'; put @4 Ligne;
Importer un fichier plat (suite) dsid = open('Attributs','i'); do Roule = 1 to attrn(dsid,'nobs'); Posit = 10; rc = fetch(dsid); Variable = getvarc(dsid,varnum(dsid,'NAME')); Format = getvarc(dsid,varnum(dsid,'Format')); if substr(Format,1,1) = '$' then Type = '$'; else Type = ''; if Roule = 1 then do; Posit = 4; Ligne = 'input '||compress(Variable)||' '||Type; end; else Ligne = compress(Variable)||' '||Type; if Roule = attrn(dsid,'nobs') then Ligne = trim(Ligne)||';'; put @Posit Ligne; end; rc = close(dsid);
Importer un fichier plat (suite) put 'run;'; run; %include Put_File;
Importer un fichier platCode généré data Source; infile "/home/gen00279/LaCapitale/BackUps/Source.txt" firstobs=2 delimiter=';' MISSOVER DSD lrecl=5000; length ZIP 8. Y 8. X 8. ZIP_CLASS $1. CITY $35. STATE 8. STATECODE $2. STATENAME $25. COUNTY 8. COUNTYNM $25. MSA 8. AREACODE 8. TIMEZONE $9. GMTOFFSET 8. DST $1. PONAME $28.; input ZIP Y X ZIP_CLASS $ CITY $ STATE STATECODE $ …. TIMEZONE $ GMTOFFSET DST $ PONAME $; run;
Sélection aléatoire filename Put_File "/work1/Generer.sas"; data Source; Ligne = _n_; set Source; run; %let dsid=%sysfunc(open(Source)); %let NbRow=%sysfunc(attrn(&dsid,nobs)); %let rc=%sysfunc(close(&dsid));
Sélection aléatoire data _null_; length Ligne $256.; file Put_File; put "data Selection;"; put " set Source;"; do n=1 to 100; Ligne = "if _n_ = "||compress(int(ranuni(today())*&NbRow.))||" then output;"; put @4 Ligne; end; put "run;"; run; %include Put_File
Sélection aléatoireCode généré data Selection; set Source; if _n_ = 6632 then output; if _n_ = 41314 then output; if _n_ = 4395 then output; if _n_ = 11798 then output; if _n_ = 24266 then output; if _n_ = 32161 then output; …. if _n_ = 13008 then output; if _n_ = 10188 then output; if _n_ = 24245 then output; run;
Lecture du LOG • Commandes ajoutées à l’autoexec : dm log "color warning green reverse" continue; dm log "color error red reverse" continue;
Questions, commentaires. Normand LétourneauENL Info Solutions inc.normand.letourneau@enlinfosolutions.ca (514) 993-9230