DATA Step, Macro, Functions and more

Dynamically naming variable names

Reply
Occasional Contributor
Posts: 8

Dynamically naming variable names

[ Edited ]

Hello everyone,

 

I have been struggling with this problem for a few days, and I haven't found something that quite works the way I want it to work. I'm sure someone with a bit more experiance will know exactlly how to do it! Thank you for the help! Here goes!

 

I have 2 data sets, one has test name and a test name ID. 

The other is from a proc mean procedure and has student ID numbers, the average, and then every cell has the average score for that student in that test. The variable names have test_1, test_2, ect... and these correspond to the test name ID in the name data set.  The variable names in the proc mean data set are not in ordet, so I can't simpily assign it. I need to sort these values somehow, but the variable names are character type. 

I am trying to find a way to assign the names (from the name data set) to the variable names (in the proc mean data set).

 

The other thing is that I would want to keep as much of the name as possible, some names have (), or # and % signs, and I want to keep those so that we can understand how the test was measured. 

 

In my sample data there are 12 test, and I could simpily rename all the variables, but the ture data set has 300 test and like 10000 students. 

 

I welcome any suggestions! 

Thank you again! 

 

mean_table

recordstatnametest_5test_11test_7test_9test_1test_4test_8test_2test_12
1234average397.40.091004751
1235average3.29.17.30.041014.36  
1236average3.497.10.021034.5561
1237average2.69.57.90.08994.6631
1238average2.19.67.30.01984.7761
1239average3.29.370.05904.7 21

 

name_table

test_nametest_id
yellow# 21
blue#2
2
red
3
pink(30%)
4
green
5
jack.and.jill
6
yellow++
7
black
8
white
9
john?
10
cyan(blue)
11
raspberry=200
12
PROC Star
Posts: 288

Re: Dynamically naming variable names

 Have you considered not renaming all the variables, but just applying labels that carry the vital information? 

 

It really depends on what you need to do with these data, but labels might be as or more useful than variable names.

PROC Star
Posts: 7,356

Re: Dynamically naming variable names

I agree with @collinelliot. Labels would be a lot easier. Try the following:

 

proc sql noprint;
  select catt('label test_',test_id,'="',test_name,'";')
    into :labels separated by ' '
      from name_table
  ;
quit;
 
data want;
  set mean_table;
  &labels.;
run;

and then simply show the labels rather than the variable names.

 

Art, CEO, AnalystFinder.com

 

Ask a Question
Discussion stats
  • 2 replies
  • 77 views
  • 0 likes
  • 3 in conversation