Help using Base SAS procedures

rename and keep

Reply
Regular Contributor
Posts: 161

rename and keep

Hello,

my code for renaming and keeping the same variable results in only 1 variable, I don't understand why.
thank you for your help in advance

%macro renam;
num_item=7;
%do seed=1 %to 2;
%do i=1 %to &num_item;
%let extra=%eval(7+&i);
data studentB&seed;
set studentb&seed (keep=question1-question&extra RENAME=(question&i=question&extra));
run;
%end;
%end;
%mend;

this is the error:

ERROR: Not all variables in the list question1-question8 were found.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.STUDENTB1 may be incomplete. When this step was stopped there were
0 observations and 0 variables.
WARNING: Data set WORK.STUDENTB1 was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
Super Contributor
Super Contributor
Posts: 3,174

Re: rename and keep

Consider that you are reading and writing the same SAS file WORK.STUDENTB1.

Where is this macro variable resolved &num_item -- ?

How/where is &extra resolved -- ?

Also, did you do a PROC CONTENTS to confirm the SAS variables are present on the input file?

You will want to generate more SAS log diagnostic output, showing the macro compilation, mostly to start with your own desk-checking so you can see the code compile and then execute.

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MPRINT /* MLOGIC */ ;

Again, suggest reviewing the logic flow, first without a macro environment -- possibly simply COPY/PASTE two back-to-back DATA steps so you can see the processing. Also, add in the PROC CONTENTS for checking variables after each DATA step.

It's also helpful to know in your original post whether your code is new or it worked previously and now you are making a modification -- with relevant info on any changes, additions, etc.

Scott Barry
SBBWorks, Inc. Message was edited by: sbb
Frequent Contributor
Posts: 102

Re: rename and keep

I think you may have your loops nested wrong. Is this closer to what you were looking for:

data studentb1;
question1 = 1; question2 = 1; question3 = 1; question4 = 1; question5 = 1; output;
run;
data studentb2;
question1 = 1; question2 = 1; question3 = 1; question4 = 1; question5 = 1; output;
run;

%macro renam;
%let num_item=5;
%do seed = 1 %to 2;
data studentB&seed;
set studentb&seed (keep=question1-question&num_item
RENAME=(
%do i=1 %to &num_item;
%let extra=%eval(7+&i);
question&i=question&extra
%end;
));
run;
%end;
%mend;

%renam;
Super User
Posts: 10,020

Re: rename and keep

Hi.
I think proc datasets is a better choice.



Ksharp
Regular Contributor
Posts: 161

Re: rename and keep

thank you for the suggestion but Unfortunately non of the suggestions work as well as I wanted.
I ended up doing it with a long code but I am sure there is an easier way to do it.

Thank you
RG
Regular Contributor
Posts: 161

Re: rename and keep

When I do this task with the following code, although it generates the data as well as I want to it gives me the following error: why SAS expect one of the following: ;, ASCII,....?

ERROR:
SYMBOLGEN: Macro variable SEED resolves to 1
NOTE: Line generated by the macro variable "SEED".
1 studnetb1
---------
22
202
MPRINT(SIMULATION): proc sort studnetb1 by studnet_num Run;

ERROR 22-322: Syntax error, expecting one of the following: ;, ASCII, BUFFNO, DANISH, DATA, DATECOPY, DETAILS, DIAG, DUPOUT, EBCDIC, EQUALS, FINNISH, FORCE, IN, ISA, L, LEAVE, LIST, MESSAGE, MSG, NATIONAL, NODUP, NODUPKEY, NODUPKEYS, NODUPLICATE, NODUPLICATES, NODUPREC, NODUPRECS, NODUPS, NOEQUALS, NORWEGIAN, NOTHREADS, OSA, OUT, OVERWRITE, AGESIZE, PRESORTED, PSIZE, REVERSE, SIZE, SORTSEQ, SORTSIZE, SORTWKNO, SWEDISH, T, TAGSORT, TECH, TECHNIQUE, TESTHSI, THREADS, WKNO, WORKNO.

ERROR 202-322: The option or parameter is not recognized and will be ignored.


CODE:
%do i=1 %to &num_item;
%let extra=%eval(7+&i);
data studentB&seed;
set studentb&seed (RENAME=(question&i=question&extra));
run;
%end;

DATA studentc&seed;
set studentc&seed (rename=(question1=question15));
Run;

proc sort studenta&seed by student_num; run;
proc sort studnetb&seed by studnet_num; Run;

/**************MERGE 3 DATASET TO HAVE 1 DATA W/ 15 ITEMS*********************/

DATA student&seed;
MERGE studenta&seed studentb&seed;
BY student_num;
run;
Super Contributor
Super Contributor
Posts: 3,174

Re: rename and keep

Don't read too much into the diagnostic -- focus on the most basic of coding your PROC SORT statement -- refer to the DOC and you'll see the missing keyword parameter for specifying your "input" (and also optionally the "output") SAS file name.

Honestly, this is simple desk-checking work while developing your SAS program -- abandon the macro to start, get some code working, and then decide if/when you need to use the SAS macro language.

Scott Barry
SBBWorks, Inc.
Frequent Contributor
Posts: 102

Re: rename and keep

You are missing semi-colons needed to properly form your proc sorts.

proc sort studenta&seed; by student_num; run;
proc sort studnetb&seed; by studnet_num; Run;

However, I do agree with SBB

Also, this is a really poor way of solving this. You are recreating the dataset &num_item times. Putting the loop inside the rename statement like I did in my example makes much more sense. Message was edited by: Curtis Mack
Regular Contributor
Posts: 161

Re: rename and keep

Posted in reply to CurtisMack
Thank you Curtis,

I did try your code but for some reason it does not work. I am not using the Macro since my code plus some other codes are all part of a slightly bigger Macro.

Thank you
RG
Super User
Posts: 10,020

Re: rename and keep

Hi.
I gave you an example which you can refer to .
[pre]


[/pre]



%macro rename;



%*rename variable name  ;



proc sql feedback;



 select name from
dictionary.columns where libname=upcase(
"sashelp")
and memname=upcase(
"class");



 select name



  into : name1 -
: name&sqlobs
.



   from dictionary.columns where libname=upcase("sashelp")
and memname=upcase(
"class");



quit;



proc datasets
library=sashelp memtype=data
nolist;



 modify &right_dsn;



  rename %do i=1 %to &sqlobs;



           

&&name&i =
_&&name&i



        
%end;



        
;



quit;



%mend;



 



%rename

[pre]


Ksharp
[/pre]

Regular Contributor
Posts: 161

Re: rename and keep

Thank you KSharp

WOW! this is very complicated specially for a beginner like me. I first have to interpret the code to be able to use it. But thank you this is great it helps me learn more complicated codes.
RG
Ask a Question
Discussion stats
  • 10 replies
  • 606 views
  • 0 likes
  • 4 in conversation