BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JackZ295
Pyrite | Level 9

Is there a way for me to delete the original variables in an array after creating new variables? 

 

In this data step:

 

data three; 
set two; 

array answer1 {50} $ A1Q1-A1Q50;
array answer1a {50} nA1Q1-nA1Q50; 
do i=1 to 50; 
if answer1 [i]="" then answer1a [i]=.; 
else if answer1 [i]='Do not know' then answer1a [i]=.; 
else if answer1 [i]='No, strongly disagree' then answer1a [i]=1;
else if answer1 [i]='No, somewhat disagree' then answer1a [i]=2;
else if answer1 [i]='Neither agree nor disagree' then answer1 [i]=3;
else if answer1 [i]='Yes, somewhat agree' then answer1a [i]=4;
else if answer1 [i]='Yes, strongly agree' then answer1a [i]=5;
end;
run;

I am creating numeric variables defined by array answer1a to replace the character variables defined by array answer1. Is there a way for me to delete all of the variables defined in the array, answer1, so that I am only left with numeric variables in my new data set? 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
DROP A1Q1-A1Q50;

Add a DROP statement to your data step, before RUN usually.

View solution in original post

3 REPLIES 3
Reeza
Super User
DROP A1Q1-A1Q50;

Add a DROP statement to your data step, before RUN usually.
JackZ295
Pyrite | Level 9

@Reeza Thanks so much! 

ballardw
Super User

In the future you might consider addressing such issues when reading the data, especially with 50 (apparently) identical values. Custom informats created with Proc Format can address much of this at input time.

 

proc format library=work;
invalue qst
'Do not know' =. 
'No, strongly disagree' =1
'No, somewhat disagree' =2
'Neither agree nor disagree' =3
'Yes, somewhat agree' =4
'Yes, strongly agree' =5
other = _error_;
run;

data example;
   informat q1 q2 qst.;
   infile datalines dlm=',' dsd;
   input q1 q2;
datalines;
'No, strongly disagree','Yes, strongly agree'
'Yes, somewhat agree','No, somewhat disagree' 
'Neither agree nor disagree','Unexpected text'
;
run;

Note that I included an other category in the informat that treats unexpected values as an error. This can alert you to some systematic problems if the value should be present all the time.

 

If the questions are sometimes allowed to not have an answer you might consider using a special missing for the "Do not know" response such as .N and add a ' '=. to address the error that the other would otherwise report to the informat. You could differentiate if needed between the "Do not know" and not answered at all if needed.

 

If without changing you input you could modify the code of your loop to:

do i=1 to 50; 
   answer1a [i]= input(answer1 [i], qst.); 
end;

One advantage of this approach is if the next iteration of the survey adds a value such as "Refused to answer" then you only need to add the text and desired value (possibly missing) to the Proc format code, rerun the format and the body of the code doesn't need to change at all to get the new response(s) handled.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 5318 views
  • 1 like
  • 3 in conversation