CC25
CC25

Reputation: 15

How do you enter variables with numeric names in SAS?

I would like to enter the name of my variables as numbers e.g. '1950-1959' and I'm using the INPUT statement, but the output is not appearing correctly.

DATA data1;
INPUT AgeGroup$ 1950-1959 1960-1969 1970-1979 1980-1989 1990-1992 Total;
DATALINES;
20-29 1919 1808  1990  2175   154  8046
30-39 2616 4585  6580  6843  1921 22545
40-49  705 2661  5027  6597  1812 16802
50-59   38  680  2562  4836  2127 10243
60-69    0   35   606  2314   831  3786
70-79    0    0    23   467   494   984
80-89    0    0     0    12    31    43
Total 5278 9769 16788 23244 7370  62449
;
RUN;

Could you please tell me if I need to use any special characters to specify that '1950-1959' etc. are names of the numeric variable?

Thanks!

Upvotes: 1

Views: 286

Answers (2)

Tom
Tom

Reputation: 51566

You can use name literals to specify names that don't follow the normal rules, for example '1950-1959'n. Make sure that the VALIDVARNAME option is set to ANY so that SAS will allow the non-standard names. You could use standard names for the variables and use the label to store that description.

input AgeGroup :$5. period1-period6 ;
label period1 = '1950-1959' period2 = '1960-1969' ....

It would probably be more useful to store the time period into a variable instead.

data data1;
  length AgeGroup $5 Period $9 count 8;
  input AgeGroup @;
  do period='1950-1959','1960-1969','1970-1979','1980-1989','1990-1992','Total';
    input count @;
    output;
  end;
datalines;
20-29 1919 1808  1990  2175   154  8046
30-39 2616 4585  6580  6843  1921 22545
40-49  705 2661  5027  6597  1812 16802
50-59   38  680  2562  4836  2127 10243
60-69    0   35   606  2314   831  3786
70-79    0    0    23   467   494   984
80-89    0    0     0    12    31    43
Total 5278 9769 16788 23244 7370  62449
;

In that structure you can more easily filter to the data for subset of the time periods. But you could still easily create a report that displays the data in that tabular layout.

proc report data=data1;
  columns agegroup count,period ;
  define agegroup / group ;
  define period / across ' ';
  define count / ' ';
run;

Results:

  AgeGr
  oup    1950-1959  1960-1969  1970-1979  1980-1989  1990-1992      Total
  20-29       1919       1808       1990       2175        154       8046
  30-39       2616       4585       6580       6843       1921      22545
  40-49        705       2661       5027       6597       1812      16802
  50-59         38        680       2562       4836       2127      10243
  60-69          0         35        606       2314        831       3786
  70-79          0          0         23        467        494        984
  80-89          0          0          0         12         31         43
  Total       5278       9769      16788      23244       7370      62449

Upvotes: 2

Stu Sztukowski
Stu Sztukowski

Reputation: 12849

Enable extended character names with options validvarname=any, then specify each as a name literal like 'this'n:

options validvarname=any;

DATA data1;
INPUT AgeGroup$ '1950-1959'n '1960-1969'n '1970-1979'n '1980-1989'n '1990-1992'n Total;
DATALINES;
20-29 1919 1808  1990  2175   154  8046
30-39 2616 4585  6580  6843  1921 22545
40-49  705 2661  5027  6597  1812 16802
50-59   38  680  2562  4836  2127 10243
60-69    0   35   606  2314   831  3786
70-79    0    0    23   467   494   984
80-89    0    0     0    12    31    43
Total 5278 9769 16788 23244 7370  62449
;
RUN;

Most modern SAS applications automatically specify this option, but occasionally you'll run into systems that still have v7 names.

Upvotes: 0

Related Questions