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

/*Hi SAS Forum,

I have the table named "one"*/

data one;

input trt patid visit dose;

cards;

0 101 1 10

0 101 2 20

1 102 1 15

1 102 2 20

;

run;

/*I wanted to extract the field "trt" from above

table and at the same time I wanted to create a brand new variable named "which_year".  The values of field "which_year" should get 1, 2 and 3 in the three tables

being created. Below macro does the

job correctly.*/

%macro any (year_number);

proc sql;

    create table Year_&year_number as

                select          trt,

                                &year_number as which_year

                

     from    one         

     ;

quit;

%mend;

%any (1);

%any (2);

%any (3);

/*Now I have two tables like below and wanted to do a left join and wanted to do the

same thing above   */

data one;

input trt patid visit dose;

cards;

0 101 1 10

0 101 2 20

1 102 1 15

1 102 2 20

;

run;

data two;

input patid sex $ 5-6 ;

cards;

101 F

102 M

103 M

105 F

;

run;

/*Below code does the job correctly */

%macro any (year_number);

proc sql;

    create table Year_&year_number as select

                a.*  ,

                b.sex

                

     from    one              a left join

             two              b

                on

                a.patid=b.patid;

quit;

%mend;

%any (1);

%any (2);

%any (3);

Question:

In the yellow highlighted table above, can’t I create a brand new variable named "which_year" which has values 1, 2 and 3 in the three tables being created.?

Thanks

Mirisa

1 ACCEPTED SOLUTION

Accepted Solutions
DBailey
Lapis Lazuli | Level 10

Wouldn't it just be:

%macro any (year_number);

proc sql;

    create table Year_&year_number as select

                a.*  ,

       b.sex ,

      &year_number as which_year

     from    one              a left join

             two              b

                on

                a.patid=b.patid;

quit;

%mend;

View solution in original post

2 REPLIES 2
DBailey
Lapis Lazuli | Level 10

Wouldn't it just be:

%macro any (year_number);

proc sql;

    create table Year_&year_number as select

                a.*  ,

       b.sex ,

      &year_number as which_year

     from    one              a left join

             two              b

                on

                a.patid=b.patid;

quit;

%mend;

Mirisage
Obsidian | Level 7

Hi DBailey,

Thank you very much for your suggestion which worked well.

Regards

Mirisa

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 721 views
  • 0 likes
  • 2 in conversation