Repository navigation
qsv for SAS Stata SPSS Users
Tier: Beginner
Commands covered: readstat, writestat, frequency, stats, moarstats, pivotp, sqlp, sample, validate, and the everyday data-management commands
This page is a starting point for analysts who think in SPSS, SAS or Stata terms. Open a .sav, .zsav, .dta, .sas7bdat, .xpt or .por file with readstat, work on the CSV with any qsv command, then write it back with writestat. Coded values stay codes by default. Variable labels and value labels travel beside the CSV in a JSON Schema data dictionary, along with SPSS missing-value declarations. To keep the missing codes in the cells (an SPSS 99, a Stata .a), also pass --sentinels-as. They reach the CSV from every format, but only an SPSS .sav can take them back.
No SAS, Stata or SPSS licence is needed. The table below maps tasks to commands. For the flags, follow the links.
| Task | SPSS | SAS | Stata | qsv |
|---|---|---|---|---|
| Open a data file |
GET FILE, GET STATA, GET SAS
|
LIBNAME + SET; PROC IMPORT DBMS=SAV|DTA
|
use; import spss, import sas
|
readstat (--select, --limit, --sample, --value-labels) |
| Codebook / variable info |
DISPLAY DICTIONARY, CODEBOOK
|
PROC CONTENTS |
describe, codebook, label list
|
readstat --metadata csv; readstat --dictionary; describegpt --dictionary drafts descriptions with an LLM |
| Frequencies | FREQUENCIES |
PROC FREQ |
tabulate (one-way) |
frequency (--weight, --pct-nulls, --limit 0) |
| Descriptives |
DESCRIPTIVES, EXAMINE
|
PROC MEANS, PROC UNIVARIATE
|
summarize, summarize, detail
|
stats (--everything, --percentiles, --weight); moarstats --advanced for skewness, kurtosis, Gini & more; pragmastat for outlier-robust center & spread |
| Correlations |
CORRELATIONS; NONPAR CORR
|
PROC CORR |
pwcorr; spearman, ktau
|
moarstats --bivariate (Pearson & covariance; add --bivariate-stats pearson,spearman,kendall). Writes <stem>.stats.bivariate.csv
|
| Crosstab counts | CROSSTABS |
PROC FREQ with TABLES a*b
|
tabulate a b |
pivotp b --index a --values b --agg len. (30.0.0: dathere/qsv#5124) When a and b are the only columns, --values b can be left out. Counts only, no chi-square test; moarstats --bivariate --bivariate-stats cramersv gives Cramér's V |
| SQL | — | PROC SQL |
— | sqlp |
| Filter cases | SELECT IF |
WHERE, subsetting IF
|
keep if, drop if
|
search -s col <regex>, or sqlp with a WHERE clause |
| Keep / drop variables |
DELETE VARIABLES; /KEEP, /DROP
|
KEEP, DROP
|
keep, drop
|
select (! excludes, q1-q20 ranges) |
| Sort | SORT CASES |
PROC SORT |
sort, gsort
|
sort -s col (-N numeric, -R descending) |
| Remove duplicates | — | PROC SORT NODUPKEY |
duplicates drop |
dedup -s cols (--dupes-output keeps the removed rows) |
| Merge on a key | MATCH FILES /BY |
MERGE + BY in a DATA step |
merge 1:1, merge m:1
|
joinp. Inner join by default, which drops unmatched cases; --left or --full keeps them |
| Append cases | ADD FILES |
SET a b; PROC APPEND
|
append |
cat rows (same columns, same order); cat rowskey when the columns differ |
| Draw a sample | SAMPLE |
PROC SURVEYSELECT |
sample |
sample (--stratified, --cluster, --systematic, --weighted) |
| Check the data against rules |
VALIDATEDATA (Data Preparation add-on) |
— | assert |
validate data.csv data.schema.json, using the readstat --dictionary output |
| Save as another format |
SAVE OUTFILE; EXPORT (.por) |
PROC EXPORT DBMS=SAV|DTA; LIBNAME ... XPORT
|
save; export sasxport8
|
writestat (--dictionary, --format, --lossy, --compress) |
Beyond the packages, synthesize generates a statistically similar fake dataset from the CSV, which helps when you can't share the real microdata. Read its privacy warning first.
This example uses resources/test/readstat_sentinels.sav, the 4-row SPSS file from the round-trip recipe. rating is coded 3 = "Good" and 4 = "Great", with 8 ("Refused") and 9 declared as user-missing values. income and name declare missing values too.
cp resources/test/readstat_sentinels.sav survey.sav # or curl it, see the recipeqsv readstat survey.sav --sentinels-as value --compress-numeric \
--dictionary survey.schema.json -o survey.csv
# 4 rows exported.id,income,income_null,rating,rating_null,name,name_null,plain
1,1500.5,,3,,Ana,,1
2,,99,,8,,NA,2
3,,950,4,,Cy,,3
4,,,,9,,,4rating holds the valid codes. rating_null holds the reason a value is missing, much like SPSS keeps 8 and 9 apart from system-missing. --compress-numeric writes 3, not 3.0.
qsv frequency -s rating,rating_null survey.csvfield,value,count,percentage,rank
rating,3,1,50,1
rating,4,1,50,1
rating,(NULL),2,,
rating_null,8,1,50,1
rating_null,9,1,50,1
rating_null,(NULL),2,,The percentages are SPSS's "Valid Percent": empty cells are left out of the denominator. Pass --pct-nulls to include them.
qsv stats -s income,rating survey.csv | qsv select field,type,mean,stddev,min,max,nullcountfield,type,mean,stddev,min,max,nullcount
income,Float,1500.5,0,1500.5,1500.5,3
rating,Integer,3.5,0.5,3,4,2As in SPSS, the user-missing codes stay out of the statistics, because they sit in the _null columns.
qsv validate survey.csv survey.schema.json
# All 4 records valid.
qsv writestat --dictionary survey.schema.json survey.csv -o survey2.sav
# 4 rows written to "survey2.sav".
qsv readstat --sentinels-as value --compress-numeric survey2.sav -o back.csv
diff survey.csv back.csv && echo identical
# identicalsurvey2.sav has the same value labels and missing-value declarations as the original. The round-trip recipe shows how to diff the metadata. It also edits the CSV in between and converts to Stata.
-
Codes, not labels, by default.
readstatwrites1, not"Male", sostatsandfrequencysee numbers. Pass--value-labelsfor the label text.writestatturns labels back into codes when it has the dictionary. See Codes vs. labels. -
User-missing values become empty cells unless you pass
--sentinels-as. Without it,readstatwarns how many it dropped. The dictionary still declares the missing values, but the dropped cells come back fromwritestatas system-missing. Use--sentinels-as value(the code) or--sentinels-as label(its label) to keep them. See Memory & ordering. -
Numbers arrive as floats (
1948.0), because these formats store every number as a double.--compress-numericwrites whole-number variables as integers. -
frequencyshows the top 10 values by default. Pass--limit 0for all of them, like a fullFREQUENCIEStable. -
Convert first. Other qsv commands can't read a
.savor.dtadirectly.readstatneeds a real file path, not stdin. See Current limitation. -
writestatrefuses rather than silently dropping metadata. For example, only SPSS.savfiles can declare missing values or take back--sentinels-ascolumns, and SAS transport (.xpt) files can't hold value labels.--lossywrites the file anyway and warns about each item left out.writestatcan't write.sas7bdator.zsav; write.xpt, or use--compressfor a compressed.sav. See Refuses rather than silently dropping. -
SPSS portable (
.por) files are read whole, so memory grows with the file. The other formats stream. -
Which binaries have it.
readstatandwritestatare inqsv,qsvpy*andqsvmcp, not inqsvliteorqsvdp, and not on big-endian targets. See Binary Variants. -
Both commands are experimental. Open the files
writestatwrites in the package that will read them before you rely on them.
-
Conversion & I/O →
readstatandwritestat: flags, formats and limits - Recipe: Round-Trip SAS / Stata / SPSS: edit, validate and convert, step by step
-
Aggregation & Statistics:
stats,frequency,moarstats,pragmastat -
SQL & Polars:
sqlpandpivotp -
Validation & Schema: what else
validatecan check
qsv — GitHub · Releases · Discussions · crates.io · qsv pro · Try it online · Benchmarks · DeepWiki · DOI · Licensed MIT · Third-party notices · AI Policy
qsv was made possible by datHere: standards-based, best-of-breed, open source solutions to make your Data Useful, Usable & Used.
Edit this page: Contributing to the Wiki
Home · Why qsv? · Tier legend
- Whirlwind Tour · Notebooks · Lessons & Exercises
- Benchmarks · Interactive Benchmarks
- Visualizations Gallery
- Neuro-symbolic, Localizable, Data Schematics
- Feature Flags · Goals/Non-goals · Testing
- Talks: NYC SOD 2022 · csv,conf,v8 · PyCon US 2025 · csv,conf,v9 · NYC SOD 2026
- "Have we achieved ACI?" series: 1 · 2 · 3 · 4
- Sponsor
- All Commands (index)
- Selection & Inspection
- Transform & Reshape
- Aggregation & Statistics
- Joins & Set Ops
- SQL & Polars
- Validation & Schema
- Metadata Profiling (profile)
- Conversion & I/O
- Geospatial
- Visualization (viz)
- HTTP & Web
- Get & Disk Cache
- Scripting (Luau / Python)
- Indexing, Compression & Diff
- AI & Documentation
- Recipes index
- Inspect an Unknown CSV
- Clean & Normalize
- Geographic Enrichment
- Date Enrichment
- CKAN Integration
- DCAT-US v3 for data.gov
- JSON Schema Validation
- Round-Trip SAS / Stata / SPSS
- Build a Data Pipeline
- Stats → Insights
- Data-Quality Signals with moarstats
- Group By
- Time Buckets
- Fetch & Cache
- Larger-than-RAM CSV
- Diff & Audit
- Multi-table Joins
- Synthesize Fake Data
- Performance Tuning
- Benchmarks
- Tuned csv Fork Benchmarks
- Security & Dependency Audits
- Environment Variables
- Stats Cache & Caching
- Lookup Tables
- MiniJinja Templating