Wednesday, December 10, 2014

SAS: piece of code to generate codes by SAS PUT statement

The question is from a colleague: there are 50 states and it requires to assign code to each state from a data set. In the original code there are 50 of if conditions like "if ... then ...; else if ... then ......".

What will happen if there are 500 states? Write 500 "if ... else ..." statements?

It is not a good to do in this hard coding way. If these "if" statements are necessary are necessary in the code, then the best way is to let SAS generate these codes with PUT statement.

An example is given. the following data set i is from 1 to 10, and the corresponding x and y value are given. suppose I wanna get the x and y by the value of i. The method of using SAS to generate the "if ... else ..." statement is:

data a;
    do i = 1 to 10;
        x = i + 5;
        y = i * 3;
        output;
    end;
run;

data _null_;
    set a end = last;
    if _n_ = 1 then put / "if i = " i "then x = " x "and y = "";";
    else            put /"else if i = " i " then x = " x "and y = "";";
    if last then    put /"else x = " x "and y = " y ";";
run;

The generated code is like:
if i = 1 then x = 6 and y = 3 ;
else if i = 2  then x = 7 and y = 6 ;
else if i = 3  then x = 8 and y = 9 ;
else if i = 4  then x = 9 and y = 12 ;
else if i = 5  then x = 10 and y = 15 ;
else if i = 6  then x = 11 and y = 18 ;
else if i = 7  then x = 12 and y = 21 ;
else if i = 8  then x = 13 and y = 24 ;
else if i = 9  then x = 14 and y = 27 ;
else if i = 10  then x = 15 and y = 30 ;
else x = 15 and y = 30 ;

So you don't need to hard code these in your script.


By the way, the best way to do this is not by the "if ... else ..." statements. The best way should use FORMAT in sas.

Another way is to use PROC SQL to join to get the corresponding value. 

29 comments:

  1. I cant wait to check out some of these blogs! I’ve really wanted to start learning more about cars and auto repairs lately and I think this will help a lot. I think it can save my family some money if we knew how to do some repairs at home.! Thanks again for all the options.

    bike spa services in mumbai
    house cleaning services in mumbai
    car wash services in mumbai
    car spa at doorstep in mumbai

    ReplyDelete
  2. Thanks for appreciating. Really means and inspires a lot to hear from you guys.I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..Believe me, This is very helpful for me.
    Home Spa Services in Mumbai

    ReplyDelete
  3. Somebody necessarily help to make severely posts I might state. This is the first time I frequented your website page and to this point? I surprised with the research you made to create this particular post extraordinary. Well done admin..
    Back to original

    ReplyDelete
  4. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
    Selenium Training in Chennai

    ReplyDelete
  5. This blog is having the general information. Got a creative work and this is very different one.We have to develop our creativity mind.This blog helps for this. Thank you for this blog. This is very interesting and useful.

    digital marketing company in india

    ReplyDelete
  6. This was so useful and informative. The article helped me to learn something new.

    Android Training in Chennai

    ReplyDelete


  7. Its very useful to me. Wonderful blog.. Thanks for sharing informative Post.

    Installment loans
    Payday loans
    Title loans

    ReplyDelete

  8. شركة نقل اثاث بالدمام تقدم خدمات لا حصر لها رائعة لتلبية طلبات العميل وتحقيق كل ما يتمناه في خدمات نقل العفش بالدمام فثقتنا في اعمالنا جيدة قد وضعتنا كخبراء داخل مجال نقل الاثاث حيث يتم التشاور مع حقيبة مختلطة من داخل شركة نقل عفش بالدمام عن اختيار المواد الخاصة بك والمواد أو الأشياء من على عتبة منزلك لتصل جميع ممتلكاتك بأمان إلى المكان الذي ترغب فيه
    شركة نقل الاثاث بجدة لدينا ميل الي التفكير في إعطاء نوع من الإدارات لدي شركة نقل اثاث بجدة التي تطوير انفسهم ومتابعة كل ما هو حديث في عالم نقل الاثاث المنزلي والبحث عن الطرق الحديثة التي تؤدي الي انجاز المهمة بكل سهولة والتي تترك نجاح باهر يمكن أن يساعد على ارضاء نفسية عملائنا.لدينا الميل في شركة نقل عفش بجدة إلى أن يشار جيدا إلى السيارات الحديثة التي نمتلكها علي انها ذات كفاءة عالية كما نمتلك عمال تنزيل علي اعلي مستوي كما يوجد لدينا في خدمة نقل عفش بجدة
    فنين فك وتركيب لديهم القدرة الكافية علي التعامل مع جميع انواع الاثاث كما اننا نمتاز باننا نمتلك المغلفون الذين يقومون بتغليف جميع الاثاث المنزلي للحفاظ عليه من الغبار والتكسير كل هذه العوامل التي تقدمها شركتنا نتيجة للمعيار والتنمية والعقل
    شركة نقل الاثاث بمكةتكتشف مجموعتنا الخاصة في فريق شركة نقل اثاث بمكة في خدمتك في أي وقت من اليوم وأنها مستعدة للبقاء طيلة الوقت لمساعدتك في نقل اثاث منزلك سواء كان بيتك كبير او صغير قهذه ميزة الشركات الكبيرة التي تقدم الخدمات المتميزة فنحن نأمل دائما لإعطاء أفضل انطباع من تجربة نقل ممتازة. يتم اتخاذ كل خطوة الرعاية من قبل فريقنا والتعامل مع أفضل طريقة ممكنة. لدينا في شركة نقل عفش بمكة المتخصصين هم أيضا على استعداد للرد على الاحتياجات الفورية لعملائنا الكرام.
    مستودعات تخزين الاثاث بالرياضالتعبئة والتغليف واحدة من أكثر المعترف بها في خدمة نقل وتخزين الاثاث بالرياض فخدمة التعبئة والتغليف لدي شركة تخزين اثاث بالرياض يمكن أن يحصل على انتباه الكثير من العملاء وهذا يرجع ببساطة إلى أننا نواصل الوفاء بطلباتهم. كما قمنا بتطوير وواصلنا تقديم أفضل الخدمات التي تحافظ علي ممتلكتهم في مستودعات تخزين العفش التي تكمل في الحفاظ علي الأموال المدفوعة في ممتلكتهم لحين الرجوع اليها مرة أخري
    شركة تخزين اثاث
    الشيء المثير للإعجاب بالنسبة الينا في شركة تخزين عفش بالرياض هو أن فريقنا على استعداد لخدمتك في أي وقت من اليوم لطالما كنت بحاجة لدينا فنحن نواصل إقناع عملائنا التأمين علي جميع العناصر الخاصة بهم. هذا هو السبب الرئيسي لماذا بقينا على أنها


    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete

  10. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    bmc software vendors

    ReplyDelete
  11. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    software var north america

    ReplyDelete
  12. Nice post ! Thanks for sharing valuable information with us. Keep sharing.
    big data and analytics services in north america

    ReplyDelete
  13. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    emc software vendors
    bmc software vendors
    Microsoft goldpartner
    sap crm service providers

    ReplyDelete
  14. There are many interesting information included and i can easily understand all given information.I post something on my blog to post something, or wait to post something worth saying. Keep update more information....
    3D printing companies in Chennai
    3D printing service Chennai
    3D printing service in Chennai

    ReplyDelete
  15. Thanks for the informative article.This is one of the best resources I have found in quite some time.Nicely written and great info.I really cannot thank you enough for sharing.

    Herbalife in Chennai
    Subamwellness in Chennai
    Weight loss in Chennai
    Weight gain in Chennai

    ReplyDelete
  16. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    sas training for job seekers

    ReplyDelete
  17. Thanks for the informative article.This is one of the best tips in my life. I have in quite some time.Nicely written and great info.I really cannot thank you enough for sharing.

    Solar Rooftop
    Solar Water Heater
    Solar Panel
    Solar Module
    Energy Efficient
    BLDC Fan
    Solar Power
    Power Plant
    Solar Training
    Solar Pump

    ReplyDelete
  18. Data science is a fast-moving field – if you’re pursuing a data science career, or even if you’re just interested in data-related topics, you need to invest time to keep up with the trends. Following a few top blogs is a great way to stay abreast of developments in data analysis, statistical software, data visualization, and more. These AUTOMATIONMINDS bloggers consistently offer great resources and tutorials, along with opportunities to connect with and learn from other leading data science professionals.
    DATA SCIENCE training in chennai

    ReplyDelete
  19. This is very good content you share on this blog. it's very informative and provide me future related information.
    python training institute in marathahalli | python training institute in btm

    ReplyDelete
  20. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    Devops Training courses
    Devops Training in Bangalore
    Best Devops Training in pune
    Devops interview questions and answers

    ReplyDelete
  21. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.

    Java training in Bangalore | Java training in Indira nagar

    Java training in Bangalore | Java training in Rajaji nagar

    Java training in Bangalore | Java training in Marathahalli

    Java training in Bangalore | Java training in Btm layout

    ReplyDelete