BookmarkSubscribeRSS Feed
R_A_G_
Calcite | Level 5
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
10 REPLIES 10
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
CurtisMack
Fluorite | Level 6
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;
Ksharp
Super User
Hi.
I think proc datasets is a better choice.



Ksharp
R_A_G_
Calcite | Level 5
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
R_A_G_
Calcite | Level 5
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;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
CurtisMack
Fluorite | Level 6
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
R_A_G_
Calcite | Level 5
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
Ksharp
Super User
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]

R_A_G_
Calcite | Level 5
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 2229 views
  • 0 likes
  • 4 in conversation