SAS append different file type - sas

I have 2 file first csv and second txt like
text.txt text.csv
1 eee 1,b1,c1
2 rff 2,b2,c2
3 r3r 3,b3,c3
4 344
Output dataset
1 eee
2 rff
3 r3r
4 344
1 b1 c1
3 b3 c3
Can we do it in single data step in sas?

Is this you are looking for:
proc import datafile="text.txt" out=txt dbms=tab replace;
getnames=no;
run;
proc import datafile="your_path/text.csv" out=csv dbms=csv replace;
getnames=no;
run;
data want;
set txt csv;
run;

Related

How to use filters when importing on sas

I have a very large data table on "dsv" format and i'm trying to import it on sas. However i don't have enough space to import the full table and then filter it (i've done this for smaller tables).
Is there any way to filter the data while importing it because at the end i will only use a part of that table ? If i want for example to import only rows that have the value 103 for Var2
PS: i'm using "proc import" not "data - infile..." because i don't know the exact number of columns
Var1
Var2
Var3
A10
103
Test
A02
102
Hiis
...
...
....
Thank you
You can add dataset options to the dataset listed in the OUT= option of PROC IMPORT.
Example:
filename dsv temp;
data _null_;
input (var1-var3) (:$20.);
file dsv dsd dlm='|';
put var1-var3;
cards;
Var1 Var2 Var3
A10 103 Test
A02 102 Hiis
;
proc import file=dsv dbms=csv out=want(where=(var2=102)) replace ;
delimter='|';
run;
The result is a dataset with just one observation.
NOTE: The data set WORK.WANT has 1 observations and 3 variables.
If you don't know the name of the second variable you could always just read the header row first and put the name into a macro variable.
data _null_;
infile dsv dsd dlm='|' truncover obs=1;
input (2*name) (:$32.);
call symputx('var2',nliteral(name));
run;
proc import file=dsv dbms=csv out=want(where=(&var2=102)) replace ;
delimter='|';
run;
You can add a where dataset option to the out= statement. For example:
proc import
file = 'myfile.txt'
out = want(where=(var2=103))
...;
run;

How to manipulate sas7bdat files?

I'm working with a sas7dbat file that was created erroneously and trying to fix it. When the file was created, the data was all input unto a single column instead of multiples, and I can't figure out how to manipulate it to do this. I thought I'd be able to use infile and make the dlm "|" with dsd to remove the quotations on the name column, but it seems that this problem is harder than it looks.
I basically want to turn that one column into the six it was supposed to be and delete the quotations from the names. Here's what it looks like in SAS:
SAS7dbat
And here's the datalines in case they're needed:
1 0017|2020-04-09|"Jason Nguyen"|122L|500.0|$404.82
2 0017|2020-04-09|"Jason Nguyen"|407XX|100.0|$201.95
3 0177|2020-04-05|"Glenda Johnson"|144L|100.0|$91.01
4 0177|2020-04-05|"Glenda Johnson"|188X|100.0|$70.76
5 0177|2020-04-05|"Glenda Johnson"|733|2.0|$101,230.00
6 0177|2020-04-05|"Glenda Johnson"|777|5.0|$106.29
7 1843|2020-04-03|"George Smith"|122|100.0|$60.64
8 1843|2020-04-03|"George Smith"|122L|10.0|$303.18
9 1843|2020-04-03|"George Smith"|144L|50.0|$91.01
10 1843|2020-04-03|"George Smith"|188S|3.0|$52,629.48
11 1843|2020-04-03|"George Smith"|855W|1.0|$92,210.41
12 1843|2020-04-03|"George Smith"|908X|1.0|$51,920.87
13 9888|2020-04-11|"Sharon Lu"|100W|1,000.0|$20.14
14 9888|2020-04-11|"Sharon Lu"|122|50.0|$60.64
(each line is one column inside SAS)
Go back and fix the import code would be my suggestion otherwise use the SCAN() function.
data want;
set have;
var1 = scan(variableName, 1, '|');
var2 = input(scan(variableName, 2, '|'), yymmdd.);
format var2 date9.;
var3 = dequote(scan(variableName, 3, '|'));
....
run;
Another option is to write the file as is back to a text file and then import it using the DLM='|' option.
Untested:
proc export data=have outfile='myfile.txt' dbms=dlm replace;
delimiter='';
run;
proc import out=want datafile='myfile.txt' dbms=dlm replace;
delimiter='|';
run;
Given that it's only 6 variables though you may as well write the data step for that code anyways.

how to split four columns into two tables in sas report

I have one table having 4 columns and i want to separate them into 2 table 2 columns in one table and 2 columns in another table.but both table should be below to each other.I want this in proc report format.code should be in report.
id name age gender
1 abc 21 m
2 pqr 23 f
3 qwe 25 f
4 ert 54 m
i want id and name in one table and age and gender in other table.but one below the other in ods excel.
I've split the main table into two tables using a data setp then appended them to each other, I added an extra columns called "source" in order to be differniate between the tables. if you use a Proc report you can group by "source"
Code:
*Create input data*/
data have;
input id name $ age gender $ ;
datalines;
1 abc 21 m
2 pqr 23 f
3 qwe 25 f
4 ert 54 m
;;;;
run;
/*Split / create first table*/
data table1;
set have;
source="table1: id & name";
keep source id name ;
run;
/*Split / create second table*/
data table2;
set have;
source="table2: age & gender";
keep source age gender;
run;
/*create Empty table*/
data want;
length Source $30. column1 8. column2 $10.;
run;
proc sql; delete * from want; quit;
/* Append both tables to each other*/
proc append base= want data=table1(rename=(id=column1 name=column2)) force ; run;
proc append base= want data=table2(rename=(age=column1 gender=column2)) force ; run;
/*Create Report*/
proc report data= want;
col source column1 column2 ;
define source / group;
run;
Output Table:
Report:
For data
data have;input
id name $ age gender $; datalines;
1 abc 21 m
2 pqr 23 f
3 qwe 25 f
4 ert 54 m
run;
Being output as Excel, the splitting into two parts can be done via two Proc REPORT steps; each step responsible for a single set of columns. Options are used in the ODS EXCEL to control how sheet processing is handled.
The first step manages the common header through DEFINE, the subsequent steps are NOHEADER and don't need DEFINE statements. Each step must define and compute the value of the new source column. There will be a one Excel row gap between each table.
ods _all_ close;
ods excel file='want.xlsx' options(sheet_interval='NONE');
proc report data=have;
column source id name;
define id / 'Column 1';
define name / 'Column 2';
define source / format=$20.;
compute source / character length=20; source='ID and NAME'; endcomp;
run;
proc report data=have noheader;
column source age gender;
define source / format=$20.;
compute source / character length=20; source='AGE and GENDER'; endcomp;
run;
ods excel close;
There is no reasonable single Proc REPORT step that would produce similar output from dataset have.

proc contents is truncating the values in out put dataset.How to get full values in out put dataset?

I'm trying to get all dataset names in a library in to a data set.
proc datasets library=LIB1 memtype=data ;
contents data=_all_ noprint out=Datasets_in_Lib1(keep=memname) ;
run;
The final data set (Datasets_in_Lib1) is having all the data set names that are in LIB1, but names are truncated to 6 characters.Is there any way to get full names of the datasets with out truncation.
Ex: If dataset name is x123456789, the Datasets_in_Lib1 will have x12345 only.
Thanks in advance,
Sam.
Agree with the comment, in 9.3 memname is $32. I don't think there was a version of SAS where data set names were limited to 6 characters. They went from 8 characters to 32 characters in v7 (I think).
Here's a log from running your code, showing it works as you want in 9.3
51 data work.x123456789;
52 x=1;
53 run;
NOTE: The data set WORK.X123456789 has 1 observations and 1 variables.
54
55 proc datasets library=work memtype=data nolist;
56 contents data=_all_ noprint out=Datasets_in_Lib1(keep=memname) ;
57 run;
NOTE: The data set WORK.DATASETS_IN_LIB1 has 1 observations and 1 variables.
58
59 data _null_;
60 set Datasets_in_Lib1;
61 put _all_;
62 run;
MEMNAME=X123456789 _ERROR_=0 _N_=1
NOTE: There were 1 observations read from the data set WORK.DATASETS_IN_LIB1.
You can also query the sashelp.vtable to obtain the list of datasets:
proc sql;
create table mem_list as
select memname
from sashelp.vtable
where libname='LIB1' and memtype='DATA';
quit;

Suppress column headings in proc report

My boss would like me to create a chart and table in SAS similar to something you can produce in excel, where the data table sits below the chart. This would mean using the data on the x-axis and placing more data below it.
Desired output
(chart area) (Row 1) Building 1 Building 2 Building 3 Building 4
(Row 2) 333 267 234 235
(Row 3) 3232 213 3215 657
I'm not sure how to do this in proc report, where the data runs long, instead of wide. Also, the data set is long:
Building ID var1 var2
Building 1 333 3232
Building 2 267 213
CarolinaJay's suggestion of a PROC GCHART or SGPLOT or whatnot followed by another proc is the way to go, IMO; while you could do both at once, it's a lot more work to do so.
To accomplish your specific table, I recommend PROC TABULATE; it doesn't care what direction your data goes.
data have;
informat buildingID $12.;
input BuildingID $ var1 var2;
datalines;
Building1 333 3232
Building2 267 213
;;;;
run;
proc tabulate data=have;
class buildingID;
var var1 var2;
tables (var1 var2)*sum=' ', buildingID=' ';
run;
Plop that under a plot, and you have something like this (I have no idea how to plot this so I just picked something totally at random):
ods _all_ close;
ods html;
data have;
informat buildingID $12.;
input BuildingID $ var1 var2;
datalines;
Building1 333 323
Building2 267 213
;;;;
run;
proc sgplot data=have;
vbar var1/response=var2 group=buildingID;
run;
title;
proc tabulate data=have;
class buildingID;
var var1 var2;
tables (var1 var2)*sum=' ', buildingID=' ';
run;
ods html close;