SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gstullo
Obsidian | Level 7

I'm sure that this is simple but I'm new to loops and iterations. After spending hours getting nowhere I figured I'd try here.

I would like to rename a set of variables as follows

ExampleA = ExampleA1;

ExampleB = ExampleB1;

output;


But I want the above to do this for 1-9.


easy enough to just copy and change the final number manually but is really like to use a loop or Marco or something...


Any thoughts?


Thanks

G




1 ACCEPTED SOLUTION

Accepted Solutions
twocanbazza
Quartz | Level 8

I think you need to explain the desire results a little better...

However simple do loop (needs to be enclosed in a Macro and data step)

%do i=1 %to 9 ;

     ExampleB&i. = ExampleA&i. ;

%end;

From a simple google  "http://support.sas.com/resources/papers/proceedings11/113-2011.pdf"

View solution in original post

4 REPLIES 4
Astounding
PROC Star

I think you are saying that you have 18 existing variables:

ExampleA1 ... ExampleA9

ExampleB1 ... ExampleB9

And you would like to change them into 9 observations with just 2 variables on each observation.  If that's the problem you're trying to solve, this would be a viable approach:

data want;

set have;

array a {9} ExampleA1 - ExampleA9;

array b {9} ExampleB1 - ExampleB9;

do _n_=1 to 9;

   ExampleA = a{_n_};

   ExampleB = b{_n_};

   output;

end;

keep ExampleA ExampleB;

run;

Of course, if you're trying to accomplish something different, that's another story.

Good luck.

gstullo
Obsidian | Level 7

Thanks for the reply.  You stated my intention correctly.

I came up with the following which gave me what I wanted though I found it slightly more cumbersome to program.  If you would no mind having a look at this as well as the Macro version that I posted in reply to Barry and letting me know if you see a benefit to one version over another, I'd appreciate it.

This was great though as I've been meaning to learn arrays.  I'll be dissecting this and learning how it works tomorrow:

data ArrayTest (keep=SubjectID VisitDate Initials Age TeamReferred SelfReferred

    PfPositive PfDensity EpisodeNumber Antimalarials Comments);

  set data9;

  array a {5} MalariaLogID1-MalariaLogID5;

  array b {5} MalariaEpisodeDate1-MalariaEpisodeDate5;

  array c {5} PatientInitials1-PatientInitials5;

  array d {5} PatientAge1-PatientAge5;

  array e {5} ReferredByStudyTeam1-ReferredByStudyTeam5;

  array f {5} SelfReferral1-SelfReferral5;

  array g {5} PfPositive1-PfPositive5;

  array h {5} PfDens1-PfDens5;

  array i {5} EpisodeNumber1-EpisodeNumber5;

  array j {5} AntimalarialProvided1-AntimalarialProvided5;

  array k {5} Comments1-Comments5;

  do n =1 to 5;

  SubjectID=a{n};

  VisitDate=b{n};

  Initials=c{n};

  Age=d{n};

  TeamReferred=e{n};

  SelfReferred=f{n};

  PfPositive=g{n};

  PfDensity=h{n};

  EpisodeNumber=i{n};

  Antimalarials=j{n};

  Comments=k{n};

  output;

  format VisitDate MMDDYY10.;

  format SubjectID ML_format.;

run;

data ArrayTest;

  set ArrayTest;

  if SubjectID="" then delete;

run;

proc sort data=Arraytest;

  by SubjectID;

run;

twocanbazza
Quartz | Level 8

I think you need to explain the desire results a little better...

However simple do loop (needs to be enclosed in a Macro and data step)

%do i=1 %to 9 ;

     ExampleB&i. = ExampleA&i. ;

%end;

From a simple google  "http://support.sas.com/resources/papers/proceedings11/113-2011.pdf"

gstullo
Obsidian | Level 7

Thanks for the tip.

I had read that article and, at first thought that my leaving the "." out after the "i" was causing this not to work.  With or without the period, I was getting the same result of 176 observations instead of the expected 848.  I finally noticed that my output statement was outside of the Do loop rather than within it which created the missing observation problem.  Once I fixed that, I got the expected results.

To answer your question, user Astounding was correct in assuming that I had several variables 1-5 (not 9) that I wanted condensed.  My initial code worked fine but the Macro version is shorter less error prone.  I've posted the initial code followed by the working macro version:

INITIAL (obnoxiously long) CODE:

data Log (keep=SubjectID VisitDate Initials Age TeamReferred SelfReferred PfPositive PfDensity EpisodeNumber Antimalarials Comments);

  set data9;

  SubjectId=MalariaLogID1;

  VisitDate=MalariaEpisodeDate1;

  Initials=PatientInitials1;

  Age=PatientAge1;

  TeamReferred=ReferredByStudyTeam1;

  SelfReferred=SelfReferral1;

  PfPositive=PfPositive1;

  PfDensity=PfDens1;

  EpisodeNumber=EpisodeNumber1;

  Antimalarials=AntimalarialProvided1;

  Comments=Comments1;

  output;

  SubjectId=MalariaLogID2;

  VisitDate=MalariaEpisodeDate2;

  Initials=PatientInitials2;

  Age=PatientAge2;

  TeamReferred=ReferredByStudyTeam2;

  SelfReferred=SelfReferral2;

  PfPositive=PfPositive2;

  PfDensity=PfDens2;

  EpisodeNumber=EpisodeNumber2;

  Antimalarials=AntimalarialProvided2;

  Comments=Comments2;

  output;

  SubjectId=MalariaLogID3;

  VisitDate=MalariaEpisodeDate3;

  Initials=PatientInitials3;

  Age=PatientAge3;

  TeamReferred=ReferredByStudyTeam3;

  SelfReferred=SelfReferral3;

  PfPositive=PfPositive3;

  PfDensity=PfDens3;

  EpisodeNumber=EpisodeNumber3;

  Antimalarials=AntimalarialProvided3;

  Comments=Comments3;

  output;

  SubjectId=MalariaLogID4;

  VisitDate=MalariaEpisodeDate4;

  Initials=PatientInitials4;

  Age=PatientAge4;

  TeamReferred=ReferredByStudyTeam4;

  SelfReferred=SelfReferral4;

  PfPositive=PfPositive4;

  PfDensity=PfDens4;

  EpisodeNumber=EpisodeNumber4;

  Antimalarials=AntimalarialProvided4;

  Comments=Comments4;

  output;

  SubjectId=MalariaLogID5;

  VisitDate=MalariaEpisodeDate5;

  Initials=PatientInitials5;

  Age=PatientAge5;

  TeamReferred=ReferredByStudyTeam5;

  SelfReferred=SelfReferral5;

  PfPositive=PfPositive5;

  PfDensity=PfDens5;

  EpisodeNumber=EpisodeNumber5;

  Antimalarials=AntimalarialProvided5;

  Comments=Comments5;

  output;

  format VisitDate MMDDYY10.;

  format SubjectID ML_format.;

run;

data Log;

  set Log;

  if SubjectID="" then delete;

run;

proc sort data=Log;

  by SubjectID;

run;

SHORT MACRO VERSION:

%macro MacroTest;

data MacroTest (keep=SubjectID VisitDate Initials Age TeamReferred SelfReferred

    PfPositive PfDensity EpisodeNumber Antimalarials Comments);

  set data9;

  %DO i = 1 %to 5;

  SubjectId=MalariaLogID&i;

  VisitDate=MalariaEpisodeDate&i;

  Initials=PatientInitials&i;

  Age=PatientAge&i;

  TeamReferred=ReferredByStudyTeam&i;

  SelfReferred=SelfReferral&i;

  PfPositive=PfPositive&i;

  PfDensity=PfDens&i;

  EpisodeNumber=EpisodeNumber&i;

  Antimalarials=AntimalarialProvided&i;

  Comments=Comments&i;

  output;

  %end;

format VisitDate MMDDYY10.;

format SubjectID ML_format.;

run;

data MacroTest;

  set MacroTest;

  if SubjectID="" then delete;

run;

proc sort data=MacroTest;

  by SubjectID;

run;

%Mend MacroTest;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 12480 views
  • 3 likes
  • 3 in conversation