Skip to content

qsv for SAS Stata SPSS Users

Joel Natividad edited this page Nov 30, 2026 · 4 revisions

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 map

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.

Worked example: SPSS file → counts & descriptives → SPSS file

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 recipe

1. Read it, keeping the codes and the missing-value codes

qsv 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,,,4

rating 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.

2. Frequencies and descriptives on the codes

qsv frequency -s rating,rating_null survey.csv
field,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,nullcount
field,type,mean,stddev,min,max,nullcount
income,Float,1500.5,0,1500.5,1500.5,3
rating,Integer,3.5,0.5,3,4,2

As in SPSS, the user-missing codes stay out of the statistics, because they sit in the _null columns.

3. Check it, write it back, compare

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
# identical

survey2.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.

Gotchas

  • Codes, not labels, by default. readstat writes 1, not "Male", so stats and frequency see numbers. Pass --value-labels for the label text. writestat turns 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, readstat warns how many it dropped. The dictionary still declares the missing values, but the dropped cells come back from writestat as 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-numeric writes whole-number variables as integers.
  • frequency shows the top 10 values by default. Pass --limit 0 for all of them, like a full FREQUENCIES table.
  • Convert first. Other qsv commands can't read a .sav or .dta directly. readstat needs a real file path, not stdin. See Current limitation.
  • writestat refuses rather than silently dropping metadata. For example, only SPSS .sav files can declare missing values or take back --sentinels-as columns, and SAS transport (.xpt) files can't hold value labels. --lossy writes the file anyway and warns about each item left out. writestat can't write .sas7bdat or .zsav; write .xpt, or use --compress for 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. readstat and writestat are in qsv, qsvpy* and qsvmcp, not in qsvlite or qsvdp, and not on big-endian targets. See Binary Variants.
  • Both commands are experimental. Open the files writestat writes in the package that will read them before you rely on them.

See also

Home · Why qsv? · Tier legend

Quick links

Get Started

Command Reference

Cookbook

Tuning & Internals

Ecosystem

Reference

Legacy

Clone this wiki locally