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.
MeganE
Pyrite | Level 9

Hi,

 

I'd like to covert this to a proc freq for efficiency, but i can't seem to find code to be able to create a variable at the same time.  For all i know it's not allowed.  But i thought i'd ask.  I can rename stuff, keep stuff, use a where statement.  But i can't seem to find code to create a variable.

 

Convert...

        proc sql;
            create table junk.qc_hcfasaf_&year. as
            select &year. as year,
                hcfasaf,
                count(hcfasaf) as count
            from clms.allclaims_&year.
            group by hcfasaf;
        quit;        

 

to....

proc freq data=clms.allclaims_&year.;

   tables hcfasaf / out=junk.qc_hcfasaf_&year.;

run;

 

But i don't know if i can create a variable during the step.

 

Anyone?

 

Thanks
Megan

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

No you can't create a new variable in proc freq.

 

But depending on your overall goal there may be another way. It looks like you'll be running this for multiple years?

I would suggest when you append the final datasets you can extract the year from the table names using the INDSNAME option.

 

data want;
set qc_hcfasaf_2001-qc_hcfasaf_2016 indsname = source;

year = scan(source, -1, "_");

run;

View solution in original post

5 REPLIES 5
Astounding
PROC Star

While PROC FREQ doesn't support this, here are a few ideas to consider.

 

Do you really need a variable, or will some other version of the year be sufficient?  For example, YEAR could become part of both the data set label and the variable label:

 

proc freq data=clms.allclaims_&year.;

   tables hcfasaf / out=junk.qc_hcfasaf_&year. (label="Counts for &year");

   label hcfasaf = "hcfasaf for &year";

run;

 

Just be sure to use double quotes, not single quotes. 

 

If you must have a variable, you can add it temporarily to a VIEW to speed up the processing:

 

data temp / view=temp;

   set clms.allclaims_&year.;

   year = &year;

run;

proc freq data=temp;

   tables year * hcfasaf / out=junk.qc_hcfasaf_&year.;

run;

Reeza
Super User

No you can't create a new variable in proc freq.

 

But depending on your overall goal there may be another way. It looks like you'll be running this for multiple years?

I would suggest when you append the final datasets you can extract the year from the table names using the INDSNAME option.

 

data want;
set qc_hcfasaf_2001-qc_hcfasaf_2016 indsname = source;

year = scan(source, -1, "_");

run;
MeganE
Pyrite | Level 9

This is brilliant!  I've never heard of indsname before.

 

Yes, it'll be running 16 times and i'd rather have one final table than 16, but i need to tell the years apart.  🙂

 

Thanks!

ballardw
Super User

Perhaps something like:

 

proc freq data=clms.allclaims_&year.;

   tables &year*hcfasaf / norow nocol out=junk.qc_hcfasaf_&year. (rename=(&year = year));

run;

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
  • 5 replies
  • 4409 views
  • 0 likes
  • 5 in conversation