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

Dear all,

After transposing a column on weeks I would like to have it sorted properly. The columns are named wk201301, wk201302 till the current week.

I have written a code which adds the weeknumbers so I can use it in a retain step to sort the columns of week like this:

data test2;

     retain wk201301, wk201302, wk201303, wk201304;

     set test;

run;

It is too intensive to have this code manually so I have made a macro which does this for me:

proc sql noprint;
   select count(*)
      into :NObs
           from test;
quit;

proc sql noprint;
   select vname
      into :week1-:week&NObs
           from test;
quit;

except the macro is not working as it doesn't count the macro till the week it should do in this case week 21. So that I can use it in a datastep to sort the columns like:

data test;

retain &week1-&weekx;

set datasetx;

run;

How can I fix this so that the macro creates week1 till week21? Any help would be greatly appreciated.

Regards,

Rishi

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Syntax on RETAIN statement does not use commas.

No you know where to get the variable names?  In your example you are pulling from a variable named VNAME in a table named TEST, but then you are using that same table as the one that has actual data with the WKxxxxxx variables.

Once you have the variable names as data then you can use the SEPARATED BY clause on INTO store all of the names into one macro variable. (Note there are limits to the length of a macro variable).

proc sql noprint;

  select vname into :varlist separated by ' '

    from test

  ;

quit;

data want ;

  retain &varlist;

  set have;

run;


If you want to query the variables in the source dataset then you can use DICTIONARY.COLUMN view.

proc sql noprint;

  select name into :varlist separated by ' '

    from dictionary.column

    where libname='WORK' and memname='HAVE'

      and upcase(name) like 'WK%'

    order by upcase(name)

  ;

quit;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

The first problem you are hitting is that &NOBS has the wrong value in it.  It contains leading blanks.  So you would be getting an error trying to code the equivalent of:

into : week1 - week      21

There are two easy ways to fix this.  Pick one ...

Insert between the two PROC SQLs:  %let nobs = &nobs;

Change the second PROC SQL:

into : week1 - week&sqlobs

Sometimes taking care of the first problem fixes the rest, sometimes not.  If that doesn't take care of it, it would be helpful to see the results of %PUT &NOBS;

Good luck.

Tom
Super User Tom
Super User

Syntax on RETAIN statement does not use commas.

No you know where to get the variable names?  In your example you are pulling from a variable named VNAME in a table named TEST, but then you are using that same table as the one that has actual data with the WKxxxxxx variables.

Once you have the variable names as data then you can use the SEPARATED BY clause on INTO store all of the names into one macro variable. (Note there are limits to the length of a macro variable).

proc sql noprint;

  select vname into :varlist separated by ' '

    from test

  ;

quit;

data want ;

  retain &varlist;

  set have;

run;


If you want to query the variables in the source dataset then you can use DICTIONARY.COLUMN view.

proc sql noprint;

  select name into :varlist separated by ' '

    from dictionary.column

    where libname='WORK' and memname='HAVE'

      and upcase(name) like 'WK%'

    order by upcase(name)

  ;

quit;

RishiThakur
Calcite | Level 5

Thanks Tom and Astounding,

It works! Thanks a lot for your help guys.

I used the code Tom provided me.

Regards,

Rishi

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!

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.

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
  • 3 replies
  • 735 views
  • 3 likes
  • 3 in conversation